-
Notifications
You must be signed in to change notification settings - Fork 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
Deprecate device.decompose_operation in cirq_ionq. #4925
Merged
CirqBot
merged 4 commits into
quantumlib:master
from
MichaelBroughton:deprecate_ionq_decompose
Feb 2, 2022
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1c90a2d
Deprecate device.decompose_operation in cirq_ionq.
MichaelBroughton 2432812
dave feedback.
MichaelBroughton 032eb31
Merge branch 'master' into deprecate_ionq_decompose
MichaelBroughton 7eb3125
Merge branch 'master' into deprecate_ionq_decompose
CirqBot 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,23 @@ | |
from typing import AbstractSet, Sequence, Union | ||
|
||
import cirq | ||
from cirq import _compat | ||
|
||
|
||
_VALID_GATES = cirq.Gateset( | ||
cirq.H, | ||
cirq.CNOT, | ||
cirq.SWAP, | ||
cirq.XPowGate, | ||
cirq.YPowGate, | ||
cirq.ZPowGate, | ||
cirq.XXPowGate, | ||
cirq.YYPowGate, | ||
cirq.ZZPowGate, | ||
cirq.MeasurementGate, | ||
unroll_circuit_op=False, | ||
accept_global_phase_op=False, | ||
) | ||
|
||
|
||
class IonQAPIDevice(cirq.Device): | ||
|
@@ -49,20 +66,6 @@ def __init__(self, qubits: Union[Sequence[cirq.LineQubit], int], atol=1e-8): | |
else: | ||
self.qubits = frozenset(qubits) | ||
self.atol = atol | ||
self.gateset = cirq.Gateset( | ||
cirq.H, | ||
cirq.CNOT, | ||
cirq.SWAP, | ||
cirq.XPowGate, | ||
cirq.YPowGate, | ||
cirq.ZPowGate, | ||
cirq.XXPowGate, | ||
cirq.YYPowGate, | ||
cirq.ZZPowGate, | ||
cirq.MeasurementGate, | ||
unroll_circuit_op=False, | ||
accept_global_phase_op=False, | ||
) | ||
|
||
def qubit_set(self) -> AbstractSet['cirq.Qid']: | ||
return self.qubits | ||
|
@@ -78,42 +81,52 @@ def validate_operation(self, operation: cirq.Operation): | |
raise ValueError(f'Operation with qubits not on the device. Qubits: {operation.qubits}') | ||
|
||
def is_api_gate(self, operation: cirq.Operation) -> bool: | ||
return operation in self.gateset | ||
return operation in _VALID_GATES | ||
|
||
@_compat.deprecated( | ||
fix='Use cirq_ionq.decompose_to_device operation instead.', | ||
deadline='v0.15', | ||
) | ||
def decompose_operation(self, operation: cirq.Operation) -> cirq.OP_TREE: | ||
if self.is_api_gate(operation): | ||
return operation | ||
assert cirq.has_unitary(operation), ( | ||
f'Operation {operation} that is not available on the IonQ API nor does it have a ' | ||
'unitary matrix to use to decompose it to the API.' | ||
) | ||
num_qubits = len(operation.qubits) | ||
if num_qubits == 1: | ||
return self._decompose_single_qubit(operation) | ||
if num_qubits == 2: | ||
return self._decompose_two_qubit(operation) | ||
raise ValueError(f'Operation {operation} not supported by IonQ API.') | ||
|
||
def _decompose_single_qubit(self, operation: cirq.Operation) -> cirq.OP_TREE: | ||
qubit = operation.qubits[0] | ||
mat = cirq.unitary(operation) | ||
for gate in cirq.single_qubit_matrix_to_gates(mat, self.atol): | ||
yield gate(qubit) | ||
|
||
def _decompose_two_qubit(self, operation: cirq.Operation) -> cirq.OP_TREE: | ||
"""Decomposes a two qubit unitary operation into ZPOW, XPOW, and CNOT.""" | ||
mat = cirq.unitary(operation) | ||
q0, q1 = operation.qubits | ||
naive = cirq.two_qubit_matrix_to_operations(q0, q1, mat, allow_partial_czs=False) | ||
temp = cirq.map_operations_and_unroll( | ||
cirq.Circuit(naive), | ||
lambda op, _: [cirq.H(op.qubits[1]), cirq.CNOT(*op.qubits), cirq.H(op.qubits[1])] | ||
if type(op.gate) == cirq.CZPowGate | ||
else op, | ||
) | ||
cirq.merge_single_qubit_gates_into_phased_x_z(temp) | ||
# A final pass breaks up PhasedXPow into Rz, Rx. | ||
yield cirq.map_operations_and_unroll( | ||
temp, | ||
lambda op, _: cirq.decompose_once(op) if type(op.gate) == cirq.PhasedXPowGate else op, | ||
).all_operations() | ||
return decompose_to_device(operation) | ||
|
||
|
||
def decompose_to_device(operation: cirq.Operation, atol: float = 1e-8) -> cirq.OP_TREE: | ||
if operation in _VALID_GATES: | ||
return operation | ||
assert cirq.has_unitary(operation), ( | ||
f'Operation {operation} that is not available on the IonQ API nor does it have a ' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove "that" |
||
'unitary matrix to use to decompose it to the API.' | ||
) | ||
num_qubits = len(operation.qubits) | ||
if num_qubits == 1: | ||
return _decompose_single_qubit(operation, atol) | ||
if num_qubits == 2: | ||
return _decompose_two_qubit(operation) | ||
raise ValueError(f'Operation {operation} not supported by IonQ API.') | ||
|
||
|
||
def _decompose_single_qubit(operation: cirq.Operation, atol: float) -> cirq.OP_TREE: | ||
qubit = operation.qubits[0] | ||
mat = cirq.unitary(operation) | ||
for gate in cirq.single_qubit_matrix_to_gates(mat, atol): | ||
yield gate(qubit) | ||
|
||
|
||
def _decompose_two_qubit(operation: cirq.Operation) -> cirq.OP_TREE: | ||
"""Decomposes a two qubit unitary operation into ZPOW, XPOW, and CNOT.""" | ||
mat = cirq.unitary(operation) | ||
q0, q1 = operation.qubits | ||
naive = cirq.two_qubit_matrix_to_operations(q0, q1, mat, allow_partial_czs=False) | ||
temp = cirq.map_operations_and_unroll( | ||
cirq.Circuit(naive), | ||
lambda op, _: [cirq.H(op.qubits[1]), cirq.CNOT(*op.qubits), cirq.H(op.qubits[1])] | ||
if type(op.gate) == cirq.CZPowGate | ||
else op, | ||
) | ||
cirq.merge_single_qubit_gates_into_phased_x_z(temp) | ||
# A final pass breaks up PhasedXPow into Rz, Rx. | ||
yield cirq.map_operations_and_unroll( | ||
temp, | ||
lambda op, _: cirq.decompose_once(op) if type(op.gate) == cirq.PhasedXPowGate else op, | ||
).all_operations() |
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
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.
adding to the import time I see :)