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

Replace OpTree with OP_TREE #6960

Merged
merged 3 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading