Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Quantum Discrete Logarithm Algorithm Implementation in the "discrete_log.ipynb" file #42

Open
4 tasks
umer066 opened this issue May 6, 2024 · 3 comments · May be fixed by #47
Open
4 tasks

Comments

@umer066
Copy link

umer066 commented May 6, 2024

Description
Optimize the implementation of the code. The suggested changes includes better variable naming, type annotations and code structure improvements

** Type of change**
Please delete options that are not relevant.

  • Refactored variable names for clarity and consistency
  • Added type annotations for function parameters and return types
  • Checked for potential optimizations in quantum and classical operations
  • Added error handling mechanisms for better reliability
from classiq.qmod import (
    CInt,
    Output,
    QArray,
    QBit,
    QNum,
    allocate,
    inplace_prepare_int,
    modular_exp,
    qfunc,
    hadamard_transform,
    invert,
    qft,
    Constraints,
    create_model,
    write_qmod,
    Preferences,
    show,
    synthesize,
    execute,
)

We can import all at once instead of importing multiple times

from math import ceil, log

We can also use ceiling and log from math

MODULUS_NUM = 5
BASE = 3
EXPONENT = 2
MODULUS_ORDER = MODULUS_NUM - 1

We can maintain consistency by using variable names like these

def discrete_log_oracle(
    base: CInt,
    exponent: CInt,
    modulus: CInt,
    order: CInt,
    x1: QArray[QBit],
    x2: QArray[QBit],
    func_res: Output[QArray[QBit]],
) -> None:
    reg_len = ceil(log(modulus, 2))
    allocate(reg_len, func_res)
    inplace_prepare_int(1, func_res)
    modular_exp(modulus, exponent, func_res, x1)
    modular_exp(modulus, base, func_res, x2)

We can improve the functions for better code readability like this.

@orsa-classiq
Copy link
Collaborator

orsa-classiq commented May 8, 2024

Thanks @umer066 for the issue!
Regarding the variable names - I agree in general. I wanted to keep the g \ x names to be as in the description of the algorithm, however it can be changed to g_generator, x_logarithm, N_modulus for clarity.

Regarding the ceil, log - they are imported from classiq because they are used symbolically in the qmod. You can see the resulting discrete_log.qmod file to better understand.

@umer066
Copy link
Author

umer066 commented May 8, 2024

Hi @orsa-classiq
Hope you are doing well.

Thanks for the response. Ok. I will check the resulting discrete_log.qmod file to better understand.
I appreciate your understanding and flexibility. To maintain clarity and consistency in the codebase, I suggest making the following adjustments:

Keep the variable names g, x, and N as per the algorithm's description, but append more descriptive names like g_generator, x_logarithm, and N_modulus where needed. This will enhance readability without deviating from the algorithm's conventions.

After making these adjustments, we'll thoroughly review the code to ensure it aligns with the algorithm's description, maintains clarity, and seamlessly integrates with the library. Extensive testing will be conducted to identify any potential issues or unexpected behavior and ensure the code's reliability.

These changes will improve the code's maintainability and readability while preserving its functionality within the qmod environment. Your approval to proceed with these adjustments would be greatly appreciated.

Looking forward to your feedback!

Best regards,

@orsa-classiq
Copy link
Collaborator

Nice, you are welcome to proceed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants