-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
created decompose protocol and assert_decompose_is_consistent_with_t_…
…complexity (#99) * created decompose protocol and assert_decompose_is_consistent_with_t_complexity * remove print * removed decompose * update decompose * update decompose * added interception and fallback decomposers to decompose_once_into_operations * remove unused import Co-authored-by: Tanuj Khattar <[email protected]>
- Loading branch information
1 parent
7073647
commit 60e8afd
Showing
7 changed files
with
182 additions
and
6 deletions.
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 |
---|---|---|
@@ -0,0 +1,92 @@ | ||
from typing import Any, Callable, Optional, Tuple | ||
|
||
import cirq | ||
from cirq.protocols.decompose_protocol import _try_decompose_into_operations_and_qubits | ||
|
||
|
||
DecomposeResult = Optional[Tuple[cirq.Operation, ...]] | ||
OpDecomposer = Callable[[Any], DecomposeResult] | ||
|
||
_FREDKIN_GATESET = cirq.Gateset(cirq.FREDKIN, unroll_circuit_op=False) | ||
|
||
|
||
def _fredkin(qubits: cirq.Qid) -> cirq.OP_TREE: | ||
"""Decomposition with 7 T and 10 clifford operations from https://arxiv.org/abs/1308.4134""" | ||
c, t1, t2 = qubits | ||
yield [cirq.CNOT(t2, t1)] | ||
yield [cirq.CNOT(c, t1), cirq.H(t2)] | ||
yield [cirq.T(c), cirq.T(t1) ** -1, cirq.T(t2)] | ||
yield [cirq.CNOT(t2, t1)] | ||
yield [cirq.CNOT(c, t2), cirq.T(t1)] | ||
yield [cirq.CNOT(c, t1), cirq.T(t2) ** -1] | ||
yield [cirq.T(t1) ** -1, cirq.CNOT(c, t2)] | ||
yield [cirq.CNOT(t2, t1)] | ||
yield [cirq.T(t1), cirq.H(t2)] | ||
yield [cirq.CNOT(t2, t1)] | ||
|
||
|
||
def _try_decompose_from_known_decompositions(val: Any) -> DecomposeResult: | ||
"""Returns a flattened decomposition of the object into operations, if possible. | ||
Args: | ||
val: The object to decompose. | ||
Returns: | ||
A flattened decomposition of `val` if it's a gate or operation with a known decomposition. | ||
""" | ||
known_decompositions = [(_FREDKIN_GATESET, _fredkin)] | ||
if not isinstance(val, (cirq.Gate, cirq.Operation)): | ||
return None | ||
|
||
classical_controls = None | ||
if isinstance(val, cirq.ClassicallyControlledOperation): | ||
classical_controls = val.classical_controls | ||
val = val.without_classical_controls() | ||
|
||
if isinstance(val, cirq.Operation): | ||
qubits = val.qubits | ||
else: | ||
qubits = cirq.LineQid.for_gate(val) | ||
|
||
for gateset, decomposer in known_decompositions: | ||
if val in gateset: | ||
decomposition = cirq.flatten_op_tree(decomposer(qubits)) | ||
if classical_controls is not None: | ||
return tuple(op.with_classical_controls(classical_controls) for op in decomposition) | ||
else: | ||
return tuple(decomposition) | ||
return None | ||
|
||
|
||
def decompose_once_into_operations( | ||
val: Any, | ||
intercepting_decomposer: Optional[OpDecomposer] = _try_decompose_from_known_decompositions, | ||
fallback_decomposer: Optional[OpDecomposer] = None, | ||
) -> DecomposeResult: | ||
"""Decomposes a value into operations, if possible. | ||
Args: | ||
val: The value to decompose into operations. | ||
intercepting_decomposer: An optional method that is called before the | ||
default decomposer (the value's `_decompose_` method). If | ||
`intercepting_decomposer` is specified and returns a result that | ||
isn't `NotImplemented` or `None`, that result is used. Otherwise the | ||
decomposition falls back to the default decomposer. | ||
fallback_decomposer: An optional decomposition that used after the | ||
`intercepting_decomposer` and the default decomposer (the value's | ||
`_decompose_` method) both fail. | ||
Returns: | ||
A tuple of operations if decomposition succeeds. | ||
""" | ||
decomposers = ( | ||
intercepting_decomposer, | ||
lambda x: _try_decompose_into_operations_and_qubits(x)[0], | ||
fallback_decomposer, | ||
) | ||
for decomposer in decomposers: | ||
if decomposer is None: | ||
continue | ||
res = decomposer(val) | ||
if res is not None: | ||
return res | ||
return None |
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,27 @@ | ||
import cirq | ||
import numpy as np | ||
from cirq_qubitization.decompose_protocol import decompose_once_into_operations, _fredkin | ||
|
||
|
||
def test_fredkin_unitary(): | ||
c, t1, t2 = cirq.LineQid.for_gate(cirq.FREDKIN) | ||
np.testing.assert_allclose( | ||
cirq.Circuit(_fredkin((c, t1, t2))).unitary(), | ||
cirq.unitary(cirq.FREDKIN(c, t1, t2)), | ||
atol=1e-8, | ||
) | ||
|
||
|
||
def test_decompose_fredkin(): | ||
c, t1, t2 = cirq.LineQid.for_gate(cirq.FREDKIN) | ||
op = cirq.FREDKIN(c, t1, t2) | ||
want = tuple(cirq.flatten_op_tree(_fredkin((c, t1, t2)))) | ||
assert want == decompose_once_into_operations(op) | ||
|
||
op = cirq.FREDKIN(c, t1, t2).with_classical_controls('key') | ||
classical_controls = op.classical_controls | ||
want = tuple( | ||
o.with_classical_controls(classical_controls) | ||
for o in cirq.flatten_op_tree(_fredkin((c, t1, t2))) | ||
) | ||
assert want == decompose_once_into_operations(op) |
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