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
341 stars 303 forks source link

Modify the mcx function to include a ctrl_state parameter, #172

Closed AbdullahKazi500 closed 3 months ago

AbdullahKazi500 commented 3 months ago

Currently, the mcx function in the classiq quantum circuit library lacks the ability to specify the control state for the multi-controlled X gate explicitly. This limits flexibility in certain quantum algorithm implementations and hardware optimization scenarios where controlling qubits in states other than '1' is advantageous.

Proposed Change: Modify the mcx function to include a ctrl_state parameter, enabling users to specify the control state of the multi-controlled X gate. This parameter should accept values such as decimal integers or bitstrings (e.g., '1', '0', '01'), defaulting to controlling the '1' state if not provided explicitly.

Expected Behavior: Upon calling the mcx function, users can pass the ctrl_state parameter to specify the desired control state configuration for the multi-controlled X gate. This enhancement will broaden the functionality of the mcx function, allowing for more precise control over quantum operations and optimizations.

Implementation Details: Function Modification: Update the mcx function to accept the ctrl_state parameter. Parameter Handling: Ensure the function correctly interprets and utilizes the ctrl_state parameter, integrating it into the construction of the multi-controlled X gate. Backward Compatibility: Maintain compatibility with existing usage patterns of the mcx function while introducing the new parameter for enhanced functionality.

@TomerGoldfriend let me know

TomerGoldfriend commented 3 months ago

@AbdullahKazi500 Classiq already supports multi-controlled operation on any quantum function, including an X gate, with any controlled state specified via an expression. For example, the following code applies an MCX with the controlled state "010":


qfunc main(output target: qbit) {
  qba: qnum;
  allocate<3>(qba);
  allocate<1>(target);
  control (qba==2) {
    X(target);
  }
}

See here for more information.

@AbdullahKazi500 please let us know if this indeed covers your issue.

AbdullahKazi500 commented 3 months ago

Currently, the mcx function in the classiq quantum circuit library lacks the ability to specify the control state for the multi-controlled X gate explicitly. This limits flexibility in certain quantum algorithm implementations and hardware optimization scenarios where controlling qubits in states other than '1' is advantageous.

Proposed Change: Modify the mcx function to include a ctrl_state parameter, enabling users to specify the control state of the multi-controlled X gate. This parameter should accept values such as decimal integers or bitstrings (e.g., '1', '0', '01'), defaulting to controlling the '1' state if not provided explicitly.

Expected Behavior: Upon calling the mcx function, users can pass the ctrl_state parameter to specify the desired control state configuration for the multi-controlled X gate. This enhancement will broaden the functionality of the mcx function, allowing for more precise control over quantum operations and optimizations.

Implementation Details: Function Modification: Update the mcx function to accept the ctrl_state parameter. Parameter Handling: Ensure the function correctly interprets and utilizes the ctrl_state parameter, integrating it into the construction of the multi-controlled X gate. Backward Compatibility: Maintain compatibility with existing usage patterns of the mcx function while introducing the new parameter for enhanced functionality.

this looks good but can we make it

qfunc apply_controlled_X(target: qbit, ctrl_register: qnum, ctrl_state: int) {
  control (ctrl_register == ctrl_state) {
    X(target);
  }
}

qfunc main() {
  qba: qnum;
  target: qbit;
  allocate<3>(qba);
  allocate(target);

  // Example usage: Apply controlled X with control state "010" (which is decimal 2)
  apply_controlled_X(target, qba, 2);
}

from this code the apply_controlled_X function encapsulates the logic for applying a controlled X gate with a specified control state, making it reusable and modular.

The ctrl_state parameter explicitly defines the control state as an integer. This makes the function more versatile, as the control state can be easily changed without modifying the internal logic. Readability and Maintainability:

The code is more readable and maintainable, with clear separation of concerns. The main function focuses on setting up and calling the apply_controlled_X function, while the latter handles the control state logic.

The function can now be easily extended to handle different control states or additional functionality, as needed. @TomerGoldfriend let me know your thoughts

TomerGoldfriend commented 3 months ago

Thank you @AbdullahKazi500 , this is a nice idea. However, currently we cannot accept this function to our open library: The apply_controlled_x is too specific and its implementation can be easily achieved via the generic control operation. If users would like to use this function they can simply include it in their specific model. For example, in https://github.com/Classiq/classiq-library/issues/56 you can add this function to the specific model in the notebook, rather than the public open library.

AbdullahKazi500 commented 3 months ago

Thank you @AbdullahKazi500 , this is a nice idea. However, currently we cannot accept this function to our open library: The apply_controlled_x is too specific and its implementation can be easily achieved via the generic control operation. If users would like to use this function they can simply include it in their specific model. For example, in #56 you can add this function to the specific model in the notebook, rather than the public open library.

I see thanks