Skip to content

Commit b9b14d7

Browse files
authored
Replace OpTree with OP_TREE (#6960)
There was a comment in `op_tree.py` to do this once mypy supports recursive types. The referenced issue python/mypy#731 is now closed, and the suggested replacement seems to work. <img width="643" alt="image" src="https://github.com/user-attachments/assets/7333a667-773c-4557-a7b7-60770b21c0a4" /> cc @maffoo
1 parent 55c592b commit b9b14d7

File tree

2 files changed

+6
-33
lines changed

2 files changed

+6
-33
lines changed

cirq-core/cirq/ops/op_tree.py

+1-27
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"""
1717

1818
from typing import Callable, Iterable, Iterator, NoReturn, Union, TYPE_CHECKING
19-
from typing_extensions import Protocol
2019

2120
from cirq._doc import document
2221
from cirq._import import LazyLoader
@@ -28,32 +27,7 @@
2827
moment = LazyLoader("moment", globals(), "cirq.circuits.moment")
2928

3029

31-
class OpTree(Protocol):
32-
"""The recursive type consumed by circuit builder methods.
33-
34-
An OpTree is a type protocol, satisfied by anything that can be recursively
35-
flattened into Operations. We also define the Union type OP_TREE which
36-
can be an OpTree or just a single Operation.
37-
38-
For example:
39-
- An Operation is an OP_TREE all by itself.
40-
- A list of operations is an OP_TREE.
41-
- A list of tuples of operations is an OP_TREE.
42-
- A list with a mix of operations and lists of operations is an OP_TREE.
43-
- A generator yielding operations is an OP_TREE.
44-
45-
Note: once mypy supports recursive types this could be defined as an alias:
46-
47-
OP_TREE = Union[Operation, Iterable['OP_TREE']]
48-
49-
See: https://github.com/python/mypy/issues/731
50-
"""
51-
52-
def __iter__(self) -> Iterator[Union[Operation, 'OpTree']]:
53-
pass
54-
55-
56-
OP_TREE = Union[Operation, OpTree]
30+
OP_TREE = Union[Operation, Iterable['OP_TREE']]
5731
document(
5832
OP_TREE,
5933
"""An operation or nested collections of operations.

cirq-core/cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Synthesis of Quantum Logic Circuits. Tech. rep. 2006,
2020
https://arxiv.org/abs/quant-ph/0406176
2121
"""
22-
from typing import List, Callable, TYPE_CHECKING
22+
from typing import Callable, Iterable, List, TYPE_CHECKING
2323

2424
from scipy.linalg import cossin
2525

@@ -38,12 +38,11 @@
3838

3939
if TYPE_CHECKING:
4040
import cirq
41-
from cirq.ops import op_tree
4241

4342

4443
def quantum_shannon_decomposition(
4544
qubits: 'List[cirq.Qid]', u: np.ndarray, atol: float = 1e-8
46-
) -> 'op_tree.OpTree':
45+
) -> Iterable['cirq.Operation']:
4746
"""Decomposes n-qubit unitary 1-q, 2-q and GlobalPhase gates, preserving global phase.
4847
4948
The gates used are CX/YPow/ZPow/CNOT/GlobalPhase/CZ/PhasedXZGate/PhasedXPowGate.
@@ -141,7 +140,7 @@ def quantum_shannon_decomposition(
141140
yield from _msb_demuxer(qubits, u1, u2)
142141

143142

144-
def _single_qubit_decomposition(qubit: 'cirq.Qid', u: np.ndarray) -> 'op_tree.OpTree':
143+
def _single_qubit_decomposition(qubit: 'cirq.Qid', u: np.ndarray) -> Iterable['cirq.Operation']:
145144
"""Decomposes single-qubit gate, and returns list of operations, keeping phase invariant.
146145
147146
Args:
@@ -186,7 +185,7 @@ def _single_qubit_decomposition(qubit: 'cirq.Qid', u: np.ndarray) -> 'op_tree.Op
186185

187186
def _msb_demuxer(
188187
demux_qubits: 'List[cirq.Qid]', u1: np.ndarray, u2: np.ndarray
189-
) -> 'op_tree.OpTree':
188+
) -> Iterable['cirq.Operation']:
190189
"""Demultiplexes a unitary matrix that is multiplexed in its most-significant-qubit.
191190
192191
Decomposition structure:
@@ -249,7 +248,7 @@ def _nth_gray(n: int) -> int:
249248

250249
def _multiplexed_cossin(
251250
cossin_qubits: 'List[cirq.Qid]', angles: List[float], rot_func: Callable = ops.ry
252-
) -> 'op_tree.OpTree':
251+
) -> Iterable['cirq.Operation']:
253252
"""Performs a multiplexed rotation over all qubits in this unitary matrix,
254253
255254
Uses ry and rz multiplexing for quantum shannon decomposition

0 commit comments

Comments
 (0)