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

[CQT-235] Add support for gate modifiers #367

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from

Conversation

rturrado
Copy link
Contributor

@rturrado rturrado commented Oct 24, 2024

Notes

  • This is very much a work in progress.
  • The current PR only passes test_gate_modifiers and test_named_gates, at test_libqasm.py.

Goal

  • This PR updates OpenSquirrel to use libQASM 0.6.8.
  • libQASM 0.6.8 introduces 2 main changes:
  1. Gate modifiers feature is implemented.
  2. Parameters are no longer part of an instruction signature.

The semantic AST that is now passed onto OpenSquirrel has this form:

  • A statement can be a variable declaration, a gate instruction, or a non-gate instruction.
  • Gate instructions correspond to unitary operations, i.e., either (named) gates (such as X), or combinations of gate modifiers acting on a (named) gate (such as ctrl.pow(2).inv.X).
  • Non-gate instructions correspond to non-unitary operations, i.e., measure or reset.
  • Gate instructions have a Gate and a list of operands. The operands are of Qubit type.
  • A Gate can have, optionally, another Gate, and also optionally, a parameter.
    Examples:
    • For inv.X, inv is a gate (modifier) that has an X gate and no parameter.
    • For pow(2).Y, pow is a gate (modifier) that has a Y gate and a 2 parameter (an exponenent of type float).
    • Similarly, X has neither gate nor parameter, and Rx has no gate but a parameter (an angle of type float).

Changes

In order to address the 2 main changes mentioned above, the current implementation of this PR has experimented with the following changes:

  1. Implement gate modiiers as functors. They are implemented in default_gate_modifiers.py. They are called {Inverse,Power,Control}GateModifier.
  2. Reimplement (named) gates as functors.

The idea behind this change from functions to functors is, basically, not to treat parameters as arguments but as members of a given instruction.
For example, for Rx, we won't be calling anymore Rx(qubit: Qubit, theta: Float). Instead, we will have an Rx(theta: Float) object and we will call Rx(qubit).
The same for PowerGateModifier.

TODO

As commented in the Notes above, the current PR has only been tested with a couple of parameterized tests.

  1. Check what errors arise from running the rest of the tests.
  2. Check if the reimplementation of (named) gates as functors causes major disruptions in the rest of the code.
    It could be that, in many parts of the code, we are just using a simple syntax such as X(q0), and this needs now a syntax X()(q0), i.e., instantiate the X functor and then invoke X.__call__(q0).
    In case it does, we should investigate if the current implementation can be improved so that a syntax such as X(q0) still works with functors.
    Or, even, we may rethink the reimplementation to not use functors.

Add GateModifierLibrary to opensquirrel/instruction_library.py.
Add test_gate_modifiers test to test/parser/libqasm/test_libqasm.py.
Implement gate_modifier decorator in opensquirrel/ir.py.
Implement inv, pow, and ctrl gate modifiers in default_gate_modifiers.py.
Gate modifiers:
- Take a Callable that returns a BlochSphereRotation.
- Invoke the Callable, together with a QubitLike argument, to obtain a BlochSphereRotation.
- Then return either a new BlochSphereRotation with a new angle (inv and pow), or a ControlledGate.

TODO: test.
…_set parameters back to Parser.

Update test_gate_modifiers.

TODO: fix parser.
TODO: from libqasm 0.6.8 (gate modifiers) gate parameters are no longer operands.
  They are parameters of the Gate node.
  We could turn all the @named_gate into functors, like InverseGateModifier.
  A functor is an instance of a class, with some members, e.g., theta for Rx,
  and a __call__operation to invoke them with some operand arguments, e.g., Rx(q).
Update Int and Float classes so that they can be constructed from another Int or Float, respectively.
Update _get_expanded_gate_args so that it does not deal with parameters anymore.
Add test_named_gates.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant