def test_z_matrix(): assert np.allclose( ops.RotZGate(half_turns=1).matrix(), np.array([[1, 0], [0, -1]])) assert np.allclose( ops.RotZGate(half_turns=0.5).matrix(), np.array([[1, 0], [0, 1j]])) assert np.allclose( ops.RotZGate(half_turns=0).matrix(), np.array([[1, 0], [0, 1]])) assert np.allclose( ops.RotZGate(half_turns=-0.5).matrix(), np.array([[1, 0], [0, -1j]]))
def test_single_qubit_matrix_to_native_gates_fuzz_half_turns_always_one_gate( pre_turns, post_turns): intended_effect = linalg.dot( ops.RotZGate(half_turns=2 * pre_turns).matrix(), ops.X.matrix(), ops.RotZGate(half_turns=2 * post_turns).matrix()) gates = decompositions.single_qubit_matrix_to_native_gates( intended_effect, tolerance=0.0001) assert len(gates) == 1 assert_gates_implement_unitary(gates, intended_effect)
def matrix(self): if not self.has_matrix(): raise ValueError("Don't have a known matrix.") phase = ops.RotZGate(half_turns=self.axis_half_turns).matrix() c = np.exp(1j * np.pi * self.half_turns) rot = np.array([[1 + c, 1 - c], [1 - c, 1 + c]]) / 2 return phase.dot(rot).dot(np.conj(phase))
def single_qubit_matrix_to_native_gates(mat: np.ndarray, tolerance: float = 0 ) -> List[ops.SingleQubitGate]: """Implements a single-qubit operation with few native gates. Args: mat: The 2x2 unitary matrix of the operation to implement. tolerance: A limit on the amount of error introduced by the construction. Returns: A list of gates that, when applied in order, perform the desired operation. """ xy_turn, xy_phase_turn, total_z_turn = ( _deconstruct_single_qubit_matrix_into_gate_turns(mat)) # Build the intended operation out of non-negligible XY and Z rotations. result = [ ExpWGate(half_turns=2 * xy_turn, axis_half_turns=2 * xy_phase_turn), ops.RotZGate(half_turns=2 * total_z_turn) ] result = [ g for g in result if protocols.trace_distance_bound(g) > tolerance ] # Special case: XY half-turns can absorb Z rotations. if len(result) == 2 and abs(xy_turn) >= 0.5 - tolerance: return [ExpWGate(axis_half_turns=2 * xy_phase_turn + total_z_turn)] return result
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 _unitary_(self) -> Union[np.ndarray, type(NotImplemented)]: if (isinstance(self.half_turns, value.Symbol) or isinstance(self.axis_half_turns, value.Symbol)): return NotImplemented phase = protocols.unitary( ops.RotZGate(half_turns=self.axis_half_turns)) c = np.exp(1j * np.pi * self.half_turns) rot = np.array([[1 + c, 1 - c], [1 - c, 1 + c]]) / 2 return np.dot(np.dot(phase, rot), np.conj(phase))
def default_decompose(self) -> ops.OP_TREE: if len(self.pauli_string) <= 0: return qubits = self.qubits any_qubit = qubits[0] to_z_ops = ops.freeze_op_tree(self.pauli_string.to_z_basis_ops()) xor_decomp = tuple(xor_nonlocal_decompose(qubits, any_qubit)) yield to_z_ops yield xor_decomp if isinstance(self.half_turns, value.Symbol): if self.pauli_string.negated: yield ops.X(any_qubit) yield ops.RotZGate(half_turns=self.half_turns)(any_qubit) if self.pauli_string.negated: yield ops.X(any_qubit) else: half_turns = self.half_turns * (-1 if self.pauli_string.negated else 1) yield ops.Z(any_qubit) ** half_turns yield protocols.inverse(xor_decomp) yield protocols.inverse(to_z_ops)
def trace_distance_bound(self) -> float: return ops.RotZGate(half_turns=self.half_turns).trace_distance_bound()
def _trace_distance_bound_(self) -> float: return protocols.trace_distance_bound( ops.RotZGate(half_turns=self.half_turns))
def test_z_extrapolate(): assert ops.RotZGate(half_turns=1).extrapolate_effect(0.5) == ops.RotZGate( half_turns=0.5) assert ops.Z**-0.25 == ops.RotZGate(half_turns=1.75)
def test_z_init(): z = ops.RotZGate(half_turns=5) assert z.half_turns == 1