def test_rot_gates_eq(): eq = EqualsTester() gates = [ ops.RotXGate, ops.RotYGate, ops.RotZGate, ops.CNotGate, ops.Rot11Gate ] for gate in gates: eq.add_equality_group(gate(half_turns=3.5), gate(half_turns=-0.5), gate(rads=-np.pi / 2), gate(degs=-90)) eq.make_equality_pair(lambda: gate(half_turns=0)) eq.make_equality_pair(lambda: gate(half_turns=0.5)) eq.add_equality_group(ops.RotXGate(), ops.RotXGate(half_turns=1), ops.X) eq.add_equality_group(ops.RotYGate(), ops.RotYGate(half_turns=1), ops.Y) eq.add_equality_group(ops.RotZGate(), ops.RotZGate(half_turns=1), ops.Z) eq.add_equality_group(ops.CNotGate(), ops.CNotGate(half_turns=1), ops.CNOT) eq.add_equality_group(ops.Rot11Gate(), ops.Rot11Gate(half_turns=1), ops.CZ)
def test_cz_matrix(): assert np.allclose( ops.Rot11Gate(half_turns=1).matrix(), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1]])) assert np.allclose( ops.Rot11Gate(half_turns=0.5).matrix(), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1j]])) assert np.allclose( ops.Rot11Gate(half_turns=0).matrix(), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]])) assert np.allclose( ops.Rot11Gate(half_turns=-0.5).matrix(), np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, -1j]]))
def default_decompose( self, qubits: Sequence[ops.QubitId]) -> ops.op_tree.OP_TREE: q0, q1 = qubits right_gate0 = CliffordGate.from_single_map(z_to=(self.pauli0, self.invert0)) right_gate1 = CliffordGate.from_single_map(z_to=(self.pauli1, self.invert1)) left_gate0 = right_gate0.inverse() left_gate1 = right_gate1.inverse() yield left_gate0(q0) yield left_gate1(q1) yield ops.Rot11Gate(half_turns=self._exponent)(q0, q1) yield right_gate0(q0) yield right_gate1(q1)
def matrix(self): if not self.has_matrix(): raise ValueError("Don't have a known matrix.") return ops.Rot11Gate(half_turns=self.half_turns).matrix()
def _unitary_(self) -> Union[np.ndarray, type(NotImplemented)]: if isinstance(self.half_turns, value.Symbol): return NotImplemented return protocols.unitary(ops.Rot11Gate(half_turns=self.half_turns))
from random import choice, sample, random from typing import Union, Sequence, TYPE_CHECKING, Dict, Optional from cirq import ops from cirq.circuits import Circuit, Moment if TYPE_CHECKING: # pylint: disable=unused-import from typing import List DEFAULT_GATE_DOMAIN = { ops.CNOT: 2, ops.CZ: 2, ops.H: 1, ops.ISWAP: 2, ops.Rot11Gate(): 2, ops.S: 1, ops.SWAP: 2, ops.T: 1, ops.X: 1, ops.Y: 1, ops.Z: 1 } # type: Dict[ops.Gate, int] def random_circuit( qubits: Union[Sequence[ops.QubitId], int], n_moments: int, op_density: float, gate_domain: Optional[Dict[ops.Gate, int]] = None) -> Circuit: """Generates a random circuit.
def test_cz_extrapolate(): assert ops.Rot11Gate( half_turns=1).extrapolate_effect(0.5) == ops.Rot11Gate(half_turns=0.5) assert ops.CZ**-0.25 == ops.Rot11Gate(half_turns=1.75)
def test_cz_repr(): assert repr(ops.Rot11Gate()) == 'CZ' assert repr(ops.Rot11Gate(half_turns=0.5)) == 'CZ**0.5' assert repr(ops.Rot11Gate(half_turns=-0.25)) == 'CZ**-0.25'
def test_cz_init(): assert ops.Rot11Gate(half_turns=0.5).half_turns == 0.5 assert ops.Rot11Gate(half_turns=5).half_turns == 1