Example #1
0
    TypeVar,
    Union,
)

import matplotlib.pyplot as plt

# this is for older systems with matplotlib <3.2 otherwise 3d projections fail
from mpl_toolkits import mplot3d  # pylint: disable=unused-import
import numpy as np

from cirq import value, protocols
from cirq._compat import proper_repr
from cirq._import import LazyLoader
from cirq.linalg import combinators, diagonalize, predicates, transformations

linalg = LazyLoader("linalg", globals(), "scipy.linalg")

if TYPE_CHECKING:
    import cirq

T = TypeVar('T')
MAGIC = np.array([[1, 0, 0, 1j], [0, 1j, 1, 0], [0, 1j, -1, 0], [1, 0, 0, -1j]
                  ]) * np.sqrt(0.5)

MAGIC_CONJ_T = np.conj(MAGIC.T)

# yapf: disable
YY = np.array([[0, 0, 0, -1],
               [0, 0, 1, 0],
               [0, 1, 0, 0],
               [-1, 0, 0, 0]])
Example #2
0
    Sequence,
    Tuple,
    TypeVar,
    TYPE_CHECKING,
    Union,
)

import numpy as np
import sympy

from cirq import protocols, value
from cirq._import import LazyLoader
from cirq.type_workarounds import NotImplementedType

# Lazy imports to break circular dependencies.
ops = LazyLoader("ops", globals(), "cirq.ops")
line_qubit = LazyLoader("line_qubit", globals(), "cirq.devices.line_qubit")


if TYPE_CHECKING:
    import cirq


class Qid(metaclass=abc.ABCMeta):
    """Identifies a quantum object such as a qubit, qudit, resonator, etc.

    Child classes represent specific types of objects, such as a qubit at a
    particular location on a chip or a qubit with a particular name.

    The main criteria that a custom qid must satisfy is *comparability*. Child
    classes meet this criteria by implementing the `_comparison_key` method. For
Example #3
0
from typing import Any, cast, Dict, List, Optional, Sequence, Tuple, TYPE_CHECKING, Union


import numpy as np

from cirq import _compat, protocols, value, linalg, qis
from cirq._import import LazyLoader
from cirq.ops import common_gates, named_qubit, raw_types, pauli_gates, phased_x_z_gate
from cirq.ops.pauli_gates import Pauli
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
    import cirq

# Lazy imports to break circular dependencies.
devices = LazyLoader("devices", globals(), "cirq.devices")
sim = LazyLoader("sim", globals(), "cirq.sim")
transformers = LazyLoader("transformers", globals(), "cirq.transformers")


@_compat.deprecated_class(deadline='v0.16', fix='Use DensePauliString instead.')
@dataclasses.dataclass
class PauliTransform:
    to: Pauli
    flip: bool


def _to_pauli_tuple(matrix: np.ndarray) -> Optional[Tuple[Pauli, bool]]:
    """Converts matrix to PauliTransform.

    If matrix is not ±Pauli matrix, returns None.
Example #4
0
import dataclasses
import functools
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Set, Tuple, Union
import numpy as np
import scipy.linalg

from cirq import devices, ops, protocols, qis
from cirq._import import LazyLoader
from cirq.devices.noise_utils import (
    PHYSICAL_GATE_TAG, )

if TYPE_CHECKING:
    import cirq

moment_module = LazyLoader("moment_module", globals(), "cirq.circuits.moment")


def _left_mul(mat: np.ndarray) -> np.ndarray:
    """Superoperator associated with left multiplication by a square matrix."""
    mat = np.asarray(mat)
    if mat.shape[-1] != mat.shape[-2]:
        raise ValueError(
            f'_left_mul only accepts square matrices, but input matrix has shape {mat.shape}.'
        )
    dim = mat.shape[-1]

    return np.kron(mat, np.eye(dim))


def _right_mul(mat: np.ndarray) -> np.ndarray:
Example #5
0
# limitations under the License.

import dataclasses
import functools
from typing import TYPE_CHECKING, Dict, List, Optional, Sequence, Set, Tuple, Union
import numpy as np
import sympy

from cirq import devices, ops, protocols, qis
from cirq._import import LazyLoader
from cirq.devices.noise_utils import PHYSICAL_GATE_TAG

if TYPE_CHECKING:
    import cirq

linalg = LazyLoader("linalg", globals(), "scipy.linalg")
moment_module = LazyLoader("moment_module", globals(), "cirq.circuits.moment")


def _left_mul(mat: np.ndarray) -> np.ndarray:
    """Superoperator associated with left multiplication by a square matrix."""
    mat = np.asarray(mat)
    if mat.shape[-1] != mat.shape[-2]:
        raise ValueError(
            f'_left_mul only accepts square matrices, but input matrix has shape {mat.shape}.'
        )
    dim = mat.shape[-1]

    return np.kron(mat, np.eye(dim))

Example #6
0
    Union,
)

import numpy as np

from cirq import protocols, ops, qis
from cirq._import import LazyLoader
from cirq.ops import raw_types, op_tree
from cirq.protocols import circuit_diagram_info_protocol
from cirq.type_workarounds import NotImplementedType

if TYPE_CHECKING:
    import cirq

# Lazy imports to break circular dependencies.
circuits = LazyLoader("circuits", globals(), "cirq.circuits.circuit")
op_tree = LazyLoader("op_tree", globals(), "cirq.ops.op_tree")
text_diagram_drawer = LazyLoader("text_diagram_drawer", globals(),
                                 "cirq.circuits.text_diagram_drawer")

TSelf_Moment = TypeVar('TSelf_Moment', bound='Moment')


def _default_breakdown(qid: 'cirq.Qid') -> Tuple[Any, Any]:
    # Attempt to convert into a position on the complex plane.
    try:
        plane_pos = complex(qid)  # type: ignore
        return plane_pos.real, plane_pos.imag
    except TypeError:
        return None, qid
Example #7
0
# See the License for the specific language governing permissions and
# limitations under the License.
"""A recursive type describing trees of operations, and utility methods for it.
"""

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
from cirq.ops.raw_types import Operation

if TYPE_CHECKING:
    import cirq

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.