-
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
Merged
tanujkhattar
merged 11 commits into
quantumlib:master
from
tanujkhattar:merge_single_qubit_gates
Feb 16, 2022
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
63a278d
Replace cirq.MergeSingleQubitGates optimizer with cirq.merge_single_q…
tanujkhattar 4778c1a
Fix isolated notebook tests
tanujkhattar 01b7eb2
Merge branch 'master' of https://github.com/quantumlib/cirq into merg…
tanujkhattar c4180d2
Merge branch 'master' of https://github.com/quantumlib/cirq into merg…
tanujkhattar 1a5bb17
Add merge_k_qubit_unitaries primitive
tanujkhattar d21f121
Fix failing mypy and pylint tests
tanujkhattar ff1fc41
Merge branch 'master' of https://github.com/quantumlib/cirq into merg…
tanujkhattar ba31c10
Fix failing diagram test
tanujkhattar 790a60f
Modify rotation values to avoid float overflow errors
tanujkhattar e1682f4
Merge branch 'master' of https://github.com/quantumlib/cirq into merg…
tanujkhattar 2db6887
Update docstring
tanujkhattar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright 2022 The Cirq Developers | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Transformer pass to merge connected components of k-qubit unitary operations.""" | ||
|
||
from typing import cast, Optional, Callable, TYPE_CHECKING | ||
|
||
from cirq import ops, protocols, circuits | ||
from cirq.transformers import transformer_api, transformer_primitives | ||
|
||
if TYPE_CHECKING: | ||
import cirq | ||
|
||
|
||
@transformer_api.transformer | ||
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': | ||
"""Merges connected components of unitary operations, acting on <= k qubits. | ||
|
||
Uses rewriter to convert a connected component of unitary operations acting on <= k-qubits | ||
into a more desirable form. If not specified, connected components are replaced by a single | ||
`cirq.MatrixGate` containing unitary matrix of the merged component. | ||
|
||
Args: | ||
circuit: Input circuit to transform. It will not be modified. | ||
context: `cirq.TransformerContext` storing common configurable options for transformers. | ||
k: Connected components of unitary operations acting on <= k qubits are merged. | ||
rewriter: Callable type that takes a `cirq.CircuitOperation`, encapsulating a connected | ||
component of unitary operations acting on <= k qubits, and produces a `cirq.OP_TREE`. | ||
Specifies how to merge the connected component into a more desirable form. | ||
|
||
Returns: | ||
Copy of the transformed input circuit. | ||
|
||
Raises: | ||
ValueError: If k <= 0 | ||
""" | ||
if k <= 0: | ||
raise ValueError(f"k should be greater than or equal to 1. Found {k}.") | ||
merged_circuit_op_tag = "_merged_k_qubit_unitaries_component" | ||
|
||
def map_func(op: 'cirq.Operation', _) -> 'cirq.OP_TREE': | ||
if not (protocols.num_qubits(op) <= k and protocols.has_unitary(op)): | ||
return op | ||
if rewriter: | ||
return rewriter( | ||
cast(circuits.CircuitOperation, op.untagged) | ||
if merged_circuit_op_tag in op.tags | ||
else circuits.CircuitOperation(circuits.FrozenCircuit(op)) | ||
) | ||
return ops.MatrixGate(protocols.unitary(op)).on(*op.qubits) | ||
|
||
circuit = transformer_primitives.merge_k_qubit_unitaries_to_circuit_op( | ||
circuit, | ||
k=k, | ||
tags_to_ignore=context.tags_to_ignore if context else (), | ||
merged_circuit_op_tag=merged_circuit_op_tag, | ||
) | ||
return transformer_primitives.map_operations_and_unroll( | ||
circuit, map_func, tags_to_ignore=context.tags_to_ignore if context else () | ||
).unfreeze(copy=False) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 replaceMergeInteractions
andMergeInteractionsToSqrtISwap
, both of which need to merge connected components of 2q gates. We can directly call this method withk=2
instead of rolling out another implementation.