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

Add cirq.merge_k_qubit_unitaries transformer to replace cirq.MergeSingleQubitGates optimizer #4986

Merged
merged 11 commits into from
Feb 16, 2022

Conversation

tanujkhattar
Copy link
Collaborator

@tanujkhattar tanujkhattar commented Feb 12, 2022

@review-notebook-app
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@CirqBot CirqBot added the size: L 250< lines changed <1000 label Feb 12, 2022
@MichaelBroughton MichaelBroughton self-assigned this Feb 14, 2022
Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a 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"
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

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(
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

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

@tanujkhattar tanujkhattar changed the title Add cirq.merge_single_qubit_gates transformer to replace cirq.MergeSingleQubitGates optimizer Add cirq.merge_k_qubit_unitaries transformer to replace cirq.MergeSingleQubitGates optimizer Feb 15, 2022
@tanujkhattar
Copy link
Collaborator Author

@MichaelBroughton This is ready for re-review. PTAL

Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a 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 ?

Comment on lines +27 to +33
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':
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

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.

Comment on lines +93 to +99
@transformer_api.transformer
def merge_single_qubit_moments_to_phxz(
circuit: 'cirq.AbstractCircuit',
*,
context: Optional['cirq.TransformerContext'] = None,
atol: float = 1e-8,
) -> 'cirq.Circuit':
Copy link
Collaborator

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 ?

Copy link
Collaborator Author

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.

Copy link
Collaborator

@MichaelBroughton MichaelBroughton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tanujkhattar tanujkhattar merged commit 7c5ad61 into quantumlib:master Feb 16, 2022
@tanujkhattar tanujkhattar deleted the merge_single_qubit_gates branch February 16, 2022 22:16
95-martin-orion pushed a commit to 95-martin-orion/Cirq that referenced this pull request Mar 2, 2022
…SingleQubitGates` optimizer (quantumlib#4986)

* Replace cirq.MergeSingleQubitGates optimizer with cirq.merge_single_qubit_gates transformer

* Add merge_k_qubit_unitaries primitive
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
…SingleQubitGates` optimizer (quantumlib#4986)

* Replace cirq.MergeSingleQubitGates optimizer with cirq.merge_single_qubit_gates transformer

* Add merge_k_qubit_unitaries primitive
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
…SingleQubitGates` optimizer (quantumlib#4986)

* Replace cirq.MergeSingleQubitGates optimizer with cirq.merge_single_qubit_gates transformer

* Add merge_k_qubit_unitaries primitive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: L 250< lines changed <1000
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants