-
Notifications
You must be signed in to change notification settings - Fork 2
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
base: develop
Are you sure you want to change the base?
Conversation
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea so far! Would be good to brainstorm the points I have left below
opensquirrel/default_gates.py
Outdated
|
||
|
||
default_bloch_sphere_rotations_without_params: list[Callable[[QubitLike], BlochSphereRotation]] | ||
default_bloch_sphere_rotations_without_params: list[type[NamedGateFunctor]] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would this not also include multi-qubit gates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. We may still have BlochSphereRotation
s as subclasses of NamedGateFunctor
s.
|
||
|
||
@named_gate | ||
def CNOT(control: QubitLike, target: QubitLike) -> ControlledGate: | ||
return ControlledGate(control, X(target)) | ||
class CNOT(NamedGateFunctor): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If gates are now classes, would the matrix representation of the gate be contained in the class itself?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could.
|
||
|
||
@named_gate | ||
def Ry(q: QubitLike, theta: Float) -> BlochSphereRotation: | ||
return BlochSphereRotation(qubit=q, axis=(0, 1, 0), angle=theta.value, phase=0) | ||
class Ry(NamedGateFunctor): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will gates possess hash, eq, contains_ etc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If needed, I don't see why not.
Notes
test_gate_modifiers
andtest_named_gates
, attest_libqasm.py
.Goal
The semantic AST that is now passed onto OpenSquirrel has this form:
X
), or combinations of gate modifiers acting on a (named) gate (such asctrl.pow(2).inv.X
).measure
orreset
.Gate
and a list of operands. The operands are ofQubit
type.Gate
can have, optionally, anotherGate
, and also optionally, a parameter.Examples:
inv.X
,inv
is a gate (modifier) that has anX
gate and no parameter.pow(2).Y
,pow
is a gate (modifier) that has aY
gate and a2
parameter (an exponent of type float).X
has neither gate nor parameter, andRx
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:
default_gate_modifiers.py
. They are called{Inverse,Power,Control}GateModifier
.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 anymoreRx(qubit: Qubit, theta: Float)
. Instead, we will have anRx(theta: Float)
object, and we will callRx(qubit)
.The same applies to
PowerGateModifier
.TODO
As commented in the Notes above, the current PR has only been tested with a couple of parametrized tests.
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 syntaxX()(q0)
, i.e., instantiate theX
functor and then invokeX.__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.