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

Use Gatesets in cirq/optimizers/* #4526

Merged
merged 2 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 6 additions & 18 deletions cirq-core/cirq/optimizers/convert_to_cz_and_single_gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,11 @@ def __init__(self, ignore_failures: bool = False, allow_partial_czs: bool = Fals
super().__init__()
self.ignore_failures = ignore_failures
self.allow_partial_czs = allow_partial_czs

def _keep(self, op: ops.Operation) -> bool:
# Check if this is a CZ
# Only keep partial CZ gates if allow_partial_czs
if isinstance(op.gate, ops.CZPowGate) and (
self.allow_partial_czs or value.canonicalize_half_turns(op.gate.exponent) == 1
):
return True

# Measurement in Z basis?
if isinstance(op.gate, ops.MeasurementGate):
return True

# SingleQubit known matrix
if len(op.qubits) == 1 and protocols.has_unitary(op):
return True
return False
self.gateset = ops.Gateset(
ops.CZPowGate if allow_partial_czs else ops.CZ,
ops.MeasurementGate,
ops.AnyUnitaryGateFamily(1),
)

def _decompose_two_qubit_unitaries(self, op: ops.Operation) -> ops.OP_TREE:
# Known matrix?
Expand All @@ -85,7 +73,7 @@ def optimization_at(
converted = protocols.decompose(
op,
intercepting_decomposer=self._decompose_two_qubit_unitaries,
keep=self._keep,
keep=self.gateset._validate_operation,
on_stuck_raise=(None if self.ignore_failures else self._on_stuck_raise),
)
if converted == [op]:
Expand Down
9 changes: 6 additions & 3 deletions cirq-core/cirq/optimizers/merge_interactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,13 +229,16 @@ def __init__(
"""
super().__init__(tolerance=tolerance, post_clean_up=post_clean_up)
self.allow_partial_czs = allow_partial_czs
self.gateset = ops.Gateset(
ops.CZPowGate if allow_partial_czs else ops.CZ,
unroll_circuit_op=False,
accept_global_phase=True,
)

def _may_keep_old_op(self, old_op: 'cirq.Operation') -> bool:
"""Returns True if the old two-qubit operation may be left unchanged
without decomposition."""
if self.allow_partial_czs:
return isinstance(old_op.gate, ops.CZPowGate)
return isinstance(old_op.gate, ops.CZPowGate) and old_op.gate.exponent == 1
return old_op in self.gateset

def _two_qubit_matrix_to_operations(
self,
Expand Down
9 changes: 6 additions & 3 deletions cirq-core/cirq/optimizers/merge_interactions_to_sqrt_iswap.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ def __init__(
super().__init__(tolerance=tolerance, post_clean_up=post_clean_up)
self.required_sqrt_iswap_count = required_sqrt_iswap_count
self.use_sqrt_iswap_inv = use_sqrt_iswap_inv
self.gateset = ops.Gateset(
ops.SQRT_ISWAP_INV if use_sqrt_iswap_inv else ops.SQRT_ISWAP,
unroll_circuit_op=False,
accept_global_phase=True,
)

def _may_keep_old_op(self, old_op: 'cirq.Operation') -> bool:
"""Returns True if the old two-qubit operation may be left unchanged
without decomposition."""
if self.use_sqrt_iswap_inv:
return isinstance(old_op.gate, ops.ISwapPowGate) and old_op.gate.exponent == -0.5
return isinstance(old_op.gate, ops.ISwapPowGate) and old_op.gate.exponent == 0.5
return old_op in self.gateset

def _two_qubit_matrix_to_operations(
self,
Expand Down