Skip to content

Commit

Permalink
Replace OpTree with OP_TREE (#6960)
Browse files Browse the repository at this point in the history
* Replace OpTree with OP_TREE

* Remove unused import from op_tree.py
  • Loading branch information
daxfohl authored Jan 23, 2025
1 parent 4206cb1 commit ebbaad6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 33 deletions.
28 changes: 1 addition & 27 deletions cirq-core/cirq/ops/op_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,32 +27,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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ebbaad6

Please sign in to comment.