def _apply_mixture_(self, args: cirq.ApplyMixtureArgs): zero_left = cirq.slice_for_qubits_equal_to(args.left_axes, 0) one_left = cirq.slice_for_qubits_equal_to(args.left_axes, 1) zero_right = cirq.slice_for_qubits_equal_to( cast(Tuple[int], args.right_axes), 0) one_right = cirq.slice_for_qubits_equal_to( cast(Tuple[int], args.right_axes), 1) args.out_buffer[:] = 0 np.copyto(dst=args.auxiliary_buffer0, src=args.target_tensor) for kraus_op in [ np.sqrt(0.5) * np.eye(2, dtype=np.complex128), np.sqrt(0.5) * x ]: np.copyto(dst=args.target_tensor, src=args.auxiliary_buffer0) cirq.apply_matrix_to_slices(args.target_tensor, kraus_op, [zero_left, one_left], out=args.auxiliary_buffer1) cirq.apply_matrix_to_slices( args.auxiliary_buffer1, np.conjugate(kraus_op), [zero_right, one_right], out=args.target_tensor, ) args.out_buffer += args.target_tensor return args.out_buffer
def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs) -> Optional[np.ndarray]: if cirq.is_parameterized(self): return NotImplemented am, bm, cm = (la.expm(-1j * self.exponent * np.array([[0, w], [w.conjugate(), 0]])) for w in self.weights) a1 = args.subspace_index(0b1001) b1 = args.subspace_index(0b0101) c1 = args.subspace_index(0b0011) a2 = args.subspace_index(0b0110) b2 = args.subspace_index(0b1010) c2 = args.subspace_index(0b1100) cirq.apply_matrix_to_slices(args.target_tensor, am, slices=[a1, a2], out=args.available_buffer) cirq.apply_matrix_to_slices(args.available_buffer, bm, slices=[b1, b2], out=args.target_tensor) return cirq.apply_matrix_to_slices(args.target_tensor, cm, slices=[c1, c2], out=args.available_buffer)
def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs ) -> Optional[np.ndarray]: if cirq.is_parameterized(self): return None am = cirq.unitary(cirq.Rx(-np.pi * self.exponent * self.weights[0])) bm = cirq.unitary(cirq.Rx(-np.pi * self.exponent * self.weights[1])) cm = cirq.unitary(cirq.Rx(-np.pi * self.exponent * self.weights[2])) a1 = args.subspace_index(0b1001) b1 = args.subspace_index(0b0101) c1 = args.subspace_index(0b0011) a2 = args.subspace_index(0b0110) b2 = args.subspace_index(0b1010) c2 = args.subspace_index(0b1100) cirq.apply_matrix_to_slices(args.target_tensor, am, slices=[a1, a2], out=args.available_buffer) cirq.apply_matrix_to_slices(args.available_buffer, bm, slices=[b1, b2], out=args.target_tensor) return cirq.apply_matrix_to_slices(args.target_tensor, cm, slices=[c1, c2], out=args.available_buffer)
def _apply_unitary_(self, args: 'cirq.ApplyUnitaryArgs') -> Optional[np.ndarray]: if cirq.is_parameterized(self): return None oi = args.subspace_index(0b01) io = args.subspace_index(0b10) ii = args.subspace_index(0b11) if self.theta != 0 or self.zeta != 0 or self.chi != 0: rx = protocols.unitary(cirq.rx(2 * self.theta)) rz1 = protocols.unitary(cirq.rz(-self.zeta + self.chi)) rz2 = protocols.unitary(cirq.rz(-self.zeta - self.chi)) inner_matrix = rz1 @ rx @ rz2 out = cirq.apply_matrix_to_slices(args.target_tensor, inner_matrix, slices=[oi, io], out=args.available_buffer) else: out = args.target_tensor if self.phi != 0: out[ii] *= cmath.exp(-1j * self.phi) if self.gamma != 0: f = cmath.exp(-1j * self.gamma) out[oi] *= f out[io] *= f out[ii] *= f * f return out
def test_apply_matrix_to_slices(): # Output is input. with pytest.raises(ValueError, match='out'): target = np.eye(2) _ = cirq.apply_matrix_to_slices(target=target, matrix=np.eye(2), out=target, slices=[0, 1]) # Wrong matrix size. with pytest.raises(ValueError, match='shape'): target = np.eye(2) _ = cirq.apply_matrix_to_slices(target=target, matrix=np.eye(3), slices=[0, 1]) # Empty case. np.testing.assert_allclose( cirq.apply_matrix_to_slices(target=np.array(range(5)), matrix=np.eye(0), slices=[]), np.array(range(5)), ) # Middle 2x2 of 4x4 case. np.testing.assert_allclose( cirq.apply_matrix_to_slices(target=np.eye(4), matrix=np.array([[2, 3], [5, 7]]), slices=[1, 2]), np.array([[1, 0, 0, 0], [0, 2, 3, 0], [0, 5, 7, 0], [0, 0, 0, 1]]), ) # Middle 2x2 of 4x4 with opposite order case. np.testing.assert_allclose( cirq.apply_matrix_to_slices(target=np.eye(4), matrix=np.array([[2, 3], [5, 7]]), slices=[2, 1]), np.array([[1, 0, 0, 0], [0, 7, 5, 0], [0, 3, 2, 0], [0, 0, 0, 1]]), ) # Complicated slices of tensor case. np.testing.assert_allclose( cirq.apply_matrix_to_slices( target=np.array(range(8)).reshape((2, 2, 2)), matrix=np.array([[0, 1], [1, 0]]), slices=[(0, slice(None), 0), (1, slice(None), 0)], ).reshape((8, )), [4, 1, 6, 3, 0, 5, 2, 7], ) # Specified output case. out = np.zeros(shape=(4, )) actual = cirq.apply_matrix_to_slices(target=np.array([1, 2, 3, 4]), matrix=np.array([[2, 3], [5, 7]]), slices=[1, 2], out=out) assert actual is out np.testing.assert_allclose(actual, np.array([1, 13, 31, 4]))
def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs ) -> Optional[np.ndarray]: if cirq.is_parameterized(self): return None inner_matrix = cirq.unitary(cirq.rx(-2 * np.pi * self.exponent)) a = args.subspace_index(0b0011) b = args.subspace_index(0b1100) return cirq.apply_matrix_to_slices(args.target_tensor, inner_matrix, slices=[a, b], out=args.available_buffer)
def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs) -> Optional[np.ndarray]: if cirq.is_parameterized(self): return None inner_matrix = cirq.unitary(cirq.Ry(-self.exponent * np.pi)) oi = args.subspace_index(0b01) io = args.subspace_index(0b10) return cirq.apply_matrix_to_slices(args.target_tensor, inner_matrix, slices=[oi, io], out=args.available_buffer)
def _apply_unitary_(self, args: 'cirq.ApplyUnitaryArgs') -> Optional[np.ndarray]: if cirq.is_parameterized(self): return None if self.theta != 0: inner_matrix = protocols.unitary(cirq.rx(2 * self.theta)) oi = args.subspace_index(0b01) io = args.subspace_index(0b10) out = cirq.apply_matrix_to_slices( args.target_tensor, inner_matrix, slices=[oi, io], out=args.available_buffer ) else: out = args.target_tensor if self.phi != 0: ii = args.subspace_index(0b11) out[ii] *= cmath.exp(-1j * self.phi) return out