Classiq / classiq-library

The Classiq Library is the largest collection of quantum algorithms and applications. It is the best way to explore quantum computing software. We welcome community contributions to our Library 🙌
https://platform.classiq.io
MIT License
327 stars 290 forks source link

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

Closed umer066 closed 3 months ago

umer066 commented 4 months ago

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.

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 commented 4 months ago

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 commented 4 months ago

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 commented 4 months ago

Nice, you are welcome to proceed