def test_identity_apply_unitary(): v = np.array([1, 0]) result = cirq.apply_unitary( cirq.I, cirq.ApplyUnitaryArgs(v, np.array([0, 1]), (0, ))) assert result is v v = np.array([1, 0, 0]) result = cirq.apply_unitary( cirq.IdentityGate(1, (3, )), cirq.ApplyUnitaryArgs(v, np.array([0, 1, 2]), (0, ))) assert result is v
def test_cast_to_complex(): y0 = cirq.PauliString({cirq.LineQubit(0): cirq.Y}) state = 0.5 * np.eye(2) args = cirq.ApplyUnitaryArgs( target_tensor=state, available_buffer=np.zeros_like(state), axes=(0,) ) with pytest.raises( np.ComplexWarning, match='Casting complex values to real discards the imaginary part' ): cirq.apply_unitary(y0, args)
def assert_works(val): expected_outputs = [ np.array([1, 1, -1, -1]).reshape((2, 2)), np.array([1, -1, 1, -1]).reshape((2, 2)), ] for axis in range(2): result = cirq.apply_unitary(val, cirq.ApplyUnitaryArgs(make_input(), buf, [axis])) np.testing.assert_allclose(result, expected_outputs[axis])
def _apply_unitary_(self, args: 'cirq.ApplyUnitaryArgs'): transposed_args = args.with_axes_transposed_to_start() target_axes = transposed_args.axes[:len(self.base_operation.qubits)] control_axes = transposed_args.axes[len(self.base_operation.qubits):] control_max = np.product([q.dimension for q in self.register]).item() for i in range(control_max): operation = self.base_operation**(self.exponent_sign * i / control_max) control_index = linalg.slice_for_qubits_equal_to( control_axes, big_endian_qureg_value=i) sub_args = cirq.ApplyUnitaryArgs( transposed_args.target_tensor[control_index], transposed_args.available_buffer[control_index], target_axes) sub_result = cirq.apply_unitary(operation, sub_args) if sub_result is not sub_args.target_tensor: sub_args.target_tensor[...] = sub_result return args.target_tensor
def _apply_unitary_(self, args): return cirq.apply_unitary(cirq.X(self.q), args)
def _apply_unitary_(self, args): return cirq.apply_unitary(cirq.X(cirq.LineQubit(0)), args)
def test_apply_unitary_presence_absence(): m = np.diag([1, -1]) class NoUnitaryEffect: pass class HasUnitary: def _unitary_(self) -> np.ndarray: return m class HasApplyReturnsNotImplemented: def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs): return NotImplemented class HasApplyReturnsNotImplementedButHasUnitary: def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs): return NotImplemented def _unitary_(self) -> np.ndarray: return m class HasApplyOutputInBuffer: def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs) -> np.ndarray: zero = args.subspace_index(0) one = args.subspace_index(1) args.available_buffer[zero] = args.target_tensor[zero] args.available_buffer[one] = -args.target_tensor[one] return args.available_buffer class HasApplyMutateInline: def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs) -> np.ndarray: one = args.subspace_index(1) args.target_tensor[one] *= -1 return args.target_tensor fails = [ NoUnitaryEffect(), HasApplyReturnsNotImplemented(), ] passes = [ HasUnitary(), HasApplyReturnsNotImplementedButHasUnitary(), HasApplyOutputInBuffer(), HasApplyMutateInline(), ] def make_input(): return np.ones((2, 2)) def assert_works(val): expected_outputs = [ np.array([1, 1, -1, -1]).reshape((2, 2)), np.array([1, -1, 1, -1]).reshape((2, 2)), ] for axis in range(2): result = cirq.apply_unitary( val, cirq.ApplyUnitaryArgs(make_input(), buf, [axis])) np.testing.assert_allclose(result, expected_outputs[axis]) buf = np.empty(shape=(2, 2), dtype=np.complex128) for f in fails: with pytest.raises(TypeError, match='failed to satisfy'): _ = cirq.apply_unitary( f, cirq.ApplyUnitaryArgs(make_input(), buf, [0])) assert (cirq.apply_unitary( f, cirq.ApplyUnitaryArgs(make_input(), buf, [0]), default=None) is None) assert (cirq.apply_unitary(f, cirq.ApplyUnitaryArgs( make_input(), buf, [0]), default=NotImplemented) is NotImplemented) assert cirq.apply_unitary(f, cirq.ApplyUnitaryArgs( make_input(), buf, [0]), default=1) == 1 for s in passes: assert_works(s) assert (cirq.apply_unitary(s, cirq.ApplyUnitaryArgs( make_input(), buf, [0]), default=None) is not None)
def _apply_unitary_(self, args): return cirq.apply_unitary(self.gate, args, default=NotImplemented)
def _apply_unitary_(self, args: cirq.ApplyUnitaryArgs) -> Optional[np.ndarray]: return cirq.apply_unitary(cirq.ControlledGate( common_gates.YXXY**self.exponent), args, default=None)
def test_subspaces_size_1(): phase_gate = cirq.MatrixGate(np.array([[1j]])) result = cirq.apply_unitary( unitary_value=phase_gate, args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((2,), dtype=np.complex64), available_buffer=cirq.eye_tensor((2,), dtype=np.complex64), axes=(0,), subspaces=[(0,)], ), ) np.testing.assert_allclose( result, np.array( [ [1j, 0], [0, 1], ] ), atol=1e-8, ) result = cirq.apply_unitary( unitary_value=phase_gate, args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((2,), dtype=np.complex64), available_buffer=cirq.eye_tensor((2,), dtype=np.complex64), axes=(0,), subspaces=[(1,)], ), ) np.testing.assert_allclose( result, np.array( [ [1, 0], [0, 1j], ] ), atol=1e-8, ) result = cirq.apply_unitary( unitary_value=phase_gate, args=cirq.ApplyUnitaryArgs( target_tensor=np.array([[0, 1], [1, 0]], dtype=np.complex64), available_buffer=np.zeros((2, 2), dtype=np.complex64), axes=(0,), subspaces=[(1,)], ), ) np.testing.assert_allclose( result, np.array( [ [0, 1], [1j, 0], ] ), atol=1e-8, )
def test_subspaces_size_3(): plus_one_mod_3_gate = cirq.XPowGate(dimension=3) result = cirq.apply_unitary( unitary_value=plus_one_mod_3_gate, args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((3,), dtype=np.complex64), available_buffer=cirq.eye_tensor((3,), dtype=np.complex64), axes=(0,), subspaces=[(0, 1, 2)], ), ) np.testing.assert_allclose( result, np.array( [ [0, 0, 1], [1, 0, 0], [0, 1, 0], ] ), atol=1e-8, ) result = cirq.apply_unitary( unitary_value=plus_one_mod_3_gate, args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((3,), dtype=np.complex64), available_buffer=cirq.eye_tensor((3,), dtype=np.complex64), axes=(0,), subspaces=[(2, 1, 0)], ), ) np.testing.assert_allclose( result, np.array( [ [0, 1, 0], [0, 0, 1], [1, 0, 0], ] ), atol=1e-8, ) result = cirq.apply_unitary( unitary_value=plus_one_mod_3_gate, args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((4,), dtype=np.complex64), available_buffer=cirq.eye_tensor((4,), dtype=np.complex64), axes=(0,), subspaces=[(1, 2, 3)], ), ) np.testing.assert_allclose( result, np.array( [ [1, 0, 0, 0], [0, 0, 0, 1], [0, 1, 0, 0], [0, 0, 1, 0], ] ), atol=1e-8, )
def test_subspace_size_2(): result = cirq.apply_unitary( unitary_value=cirq.X, args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((3,), dtype=np.complex64), available_buffer=cirq.eye_tensor((3,), dtype=np.complex64), axes=(0,), subspaces=[(0, 1)], ), ) np.testing.assert_allclose( result, np.array( [ [0, 1, 0], [1, 0, 0], [0, 0, 1], ] ), atol=1e-8, ) result = cirq.apply_unitary( unitary_value=cirq.X, args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((3,), dtype=np.complex64), available_buffer=cirq.eye_tensor((3,), dtype=np.complex64), axes=(0,), subspaces=[(0, 2)], ), ) np.testing.assert_allclose( result, np.array( [ [0, 0, 1], [0, 1, 0], [1, 0, 0], ] ), atol=1e-8, ) result = cirq.apply_unitary( unitary_value=cirq.X, args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((3,), dtype=np.complex64), available_buffer=cirq.eye_tensor((3,), dtype=np.complex64), axes=(0,), subspaces=[(1, 2)], ), ) np.testing.assert_allclose( result, np.array( [ [1, 0, 0], [0, 0, 1], [0, 1, 0], ] ), atol=1e-8, ) result = cirq.apply_unitary( unitary_value=cirq.X, args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((4,), dtype=np.complex64), available_buffer=cirq.eye_tensor((4,), dtype=np.complex64), axes=(0,), subspaces=[(1, 2)], ), ) np.testing.assert_allclose( result, np.array( [ [1, 0, 0, 0], [0, 0, 1, 0], [0, 1, 0, 0], [0, 0, 0, 1], ] ), atol=1e-8, )