-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add cirq.merge_k_qubit_unitaries
transformer to replace cirq.MergeSingleQubitGates
optimizer
#4986
Add cirq.merge_k_qubit_unitaries
transformer to replace cirq.MergeSingleQubitGates
optimizer
#4986
Conversation
…ubit_gates transformer
Check out this pull request on See visual diffs & provide feedback on Jupyter Notebooks. Powered by ReviewNB |
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.
Looking good. Just a few minor comments. Big question is: can we get away with making this API simpler by getting rid of the synthesizer argument and just calling .unitary
inside of the rewriter ?
""" | ||
if rewriter is not None and synthesizer is not None: | ||
raise ValueError("Can't specify both rewriter and synthesizer.") | ||
merged_circuit_op_tag = "_merged_single_qubit_gates_component" |
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.
Should this be a global ?
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.
No, the tag should be local because it's used as an implementation detail to identify circuit operations which were created by the internal merge and given them special treatment while passing arguments to rewriter
. Note that this tag is never applied to any operation of the final circuit being returned from the function.
|
||
|
||
@transformer_api.transformer | ||
def merge_single_qubit_gates_to_phased_x_and_z( |
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.
Do we still need this transformer ?
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 think we should still keep this transformer as it's hard to predict all the user call sites where it might be getting used and whether phsx version would be an ideal replacement or not. There's also very little overhead to keep it
…e_single_qubit_gates
…e_single_qubit_gates
…e_single_qubit_gates
cirq.merge_single_qubit_gates
transformer to replace cirq.MergeSingleQubitGates
optimizercirq.merge_k_qubit_unitaries
transformer to replace cirq.MergeSingleQubitGates
optimizer
@MichaelBroughton This is ready for re-review. PTAL |
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 looks like we added a more new functions and changed a lot of names up. Just wanted to confirm we want to add even more stuff in this PR or do we think it might be better to stick with what we originally had in the description ?
def merge_k_qubit_unitaries( | ||
circuit: 'cirq.AbstractCircuit', | ||
*, | ||
context: Optional['cirq.TransformerContext'] = None, | ||
k: int = 0, | ||
rewriter: Optional[Callable[['cirq.CircuitOperation'], 'cirq.OP_TREE']] = None, | ||
) -> 'cirq.Circuit': |
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'm a little confused now, this seems like a kind of drastic API change. What is k for if it can only be zero or one ? This also errors by default ?
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.
k
can by any integer > 0 . This will be useful to replace MergeInteractions
and MergeInteractionsToSqrtISwap
, both of which need to merge connected components of 2q gates. We can directly call this method with k=2
instead of rolling out another implementation.
@transformer_api.transformer | ||
def merge_single_qubit_moments_to_phxz( | ||
circuit: 'cirq.AbstractCircuit', | ||
*, | ||
context: Optional['cirq.TransformerContext'] = None, | ||
atol: float = 1e-8, | ||
) -> 'cirq.Circuit': |
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.
Why do we need a moment based transformer in addition to a gate based one ?
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.
See the tests for an example use case.
The idea is that if I give you a moment preserving circuit with alternating layers and 1 and 2q gates that you need to convert to a target gateset, during conversion you will often end up in a situation where there are two adjacent moments of single qubit gates. Calling an operation based transformer to merge the single qubit gates is not sufficient as it can result in 2 separate moments in the final result, whereas a moment based transformer will guarantee that the two moments are merged into one.
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.
LGTM
…e_single_qubit_gates
…SingleQubitGates` optimizer (quantumlib#4986) * Replace cirq.MergeSingleQubitGates optimizer with cirq.merge_single_qubit_gates transformer * Add merge_k_qubit_unitaries primitive
…SingleQubitGates` optimizer (quantumlib#4986) * Replace cirq.MergeSingleQubitGates optimizer with cirq.merge_single_qubit_gates transformer * Add merge_k_qubit_unitaries primitive
…SingleQubitGates` optimizer (quantumlib#4986) * Replace cirq.MergeSingleQubitGates optimizer with cirq.merge_single_qubit_gates transformer * Add merge_k_qubit_unitaries primitive
cirq.merge_k_qubit_unitaries
cirq.merge_single_qubit_gates_to_phased_x_and_z
cirq.merge_single_qubit_gates_to_phxz
cirq.merge_single_qubit_moments_to_phxz