From 9681017e3498fe725eb72a753d33cf23f871c872 Mon Sep 17 00:00:00 2001 From: daxfo Date: Sun, 19 Jan 2025 06:45:27 -0800 Subject: [PATCH 1/2] Replace OpTree with OP_TREE --- cirq-core/cirq/ops/op_tree.py | 27 +------------------ .../quantum_shannon_decomposition.py | 11 ++++---- 2 files changed, 6 insertions(+), 32 deletions(-) diff --git a/cirq-core/cirq/ops/op_tree.py b/cirq-core/cirq/ops/op_tree.py index b9d2158018d..0f4c66785bf 100644 --- a/cirq-core/cirq/ops/op_tree.py +++ b/cirq-core/cirq/ops/op_tree.py @@ -28,32 +28,7 @@ moment = LazyLoader("moment", globals(), "cirq.circuits.moment") -class OpTree(Protocol): - """The recursive type consumed by circuit builder methods. - - An OpTree is a type protocol, satisfied by anything that can be recursively - flattened into Operations. We also define the Union type OP_TREE which - can be an OpTree or just a single Operation. - - For example: - - An Operation is an OP_TREE all by itself. - - A list of operations is an OP_TREE. - - A list of tuples of operations is an OP_TREE. - - A list with a mix of operations and lists of operations is an OP_TREE. - - A generator yielding operations is an OP_TREE. - - Note: once mypy supports recursive types this could be defined as an alias: - - OP_TREE = Union[Operation, Iterable['OP_TREE']] - - See: https://github.com/python/mypy/issues/731 - """ - - def __iter__(self) -> Iterator[Union[Operation, 'OpTree']]: - pass - - -OP_TREE = Union[Operation, OpTree] +OP_TREE = Union[Operation, Iterable['OP_TREE']] document( OP_TREE, """An operation or nested collections of operations. diff --git a/cirq-core/cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py b/cirq-core/cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py index 085e59db297..8edd9e928cd 100644 --- a/cirq-core/cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py +++ b/cirq-core/cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py @@ -19,7 +19,7 @@ Synthesis of Quantum Logic Circuits. Tech. rep. 2006, https://arxiv.org/abs/quant-ph/0406176 """ -from typing import List, Callable, TYPE_CHECKING +from typing import Callable, Iterable, List, TYPE_CHECKING from scipy.linalg import cossin @@ -38,12 +38,11 @@ if TYPE_CHECKING: import cirq - from cirq.ops import op_tree def quantum_shannon_decomposition( qubits: 'List[cirq.Qid]', u: np.ndarray, atol: float = 1e-8 -) -> 'op_tree.OpTree': +) -> Iterable['cirq.Operation']: """Decomposes n-qubit unitary 1-q, 2-q and GlobalPhase gates, preserving global phase. The gates used are CX/YPow/ZPow/CNOT/GlobalPhase/CZ/PhasedXZGate/PhasedXPowGate. @@ -141,7 +140,7 @@ def quantum_shannon_decomposition( yield from _msb_demuxer(qubits, u1, u2) -def _single_qubit_decomposition(qubit: 'cirq.Qid', u: np.ndarray) -> 'op_tree.OpTree': +def _single_qubit_decomposition(qubit: 'cirq.Qid', u: np.ndarray) -> Iterable['cirq.Operation']: """Decomposes single-qubit gate, and returns list of operations, keeping phase invariant. Args: @@ -186,7 +185,7 @@ def _single_qubit_decomposition(qubit: 'cirq.Qid', u: np.ndarray) -> 'op_tree.Op def _msb_demuxer( demux_qubits: 'List[cirq.Qid]', u1: np.ndarray, u2: np.ndarray -) -> 'op_tree.OpTree': +) -> Iterable['cirq.Operation']: """Demultiplexes a unitary matrix that is multiplexed in its most-significant-qubit. Decomposition structure: @@ -249,7 +248,7 @@ def _nth_gray(n: int) -> int: def _multiplexed_cossin( cossin_qubits: 'List[cirq.Qid]', angles: List[float], rot_func: Callable = ops.ry -) -> 'op_tree.OpTree': +) -> Iterable['cirq.Operation']: """Performs a multiplexed rotation over all qubits in this unitary matrix, Uses ry and rz multiplexing for quantum shannon decomposition From b78a3a41dd2078d6640400e53d4db87d75053e37 Mon Sep 17 00:00:00 2001 From: Dax Fohl Date: Sun, 19 Jan 2025 13:39:39 -0800 Subject: [PATCH 2/2] Remove unused import from op_tree.py --- cirq-core/cirq/ops/op_tree.py | 1 - 1 file changed, 1 deletion(-) diff --git a/cirq-core/cirq/ops/op_tree.py b/cirq-core/cirq/ops/op_tree.py index 0f4c66785bf..0bbe64e2167 100644 --- a/cirq-core/cirq/ops/op_tree.py +++ b/cirq-core/cirq/ops/op_tree.py @@ -16,7 +16,6 @@ """ from typing import Callable, Iterable, Iterator, NoReturn, Union, TYPE_CHECKING -from typing_extensions import Protocol from cirq._doc import document from cirq._import import LazyLoader