def test_invalid_subspaces(): with pytest.raises(ValueError, match='Subspace specified does not exist in axis'): _ = cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((2,), dtype=np.complex64), available_buffer=cirq.eye_tensor((2,), dtype=np.complex64), axes=(0,), subspaces=[(1, 2)], ) with pytest.raises(ValueError, match='Subspace count does not match axis count'): _ = cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((2,), dtype=np.complex64), available_buffer=cirq.eye_tensor((2,), dtype=np.complex64), axes=(0,), subspaces=[(0, 1), (0, 1)], ) with pytest.raises(ValueError, match='has zero dimensions'): _ = cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((2,), dtype=np.complex64), available_buffer=cirq.eye_tensor((2,), dtype=np.complex64), axes=(0,), subspaces=[()], ) with pytest.raises(ValueError, match='does not have consistent step size'): _ = 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, 1)], )
def _unitary_(self): sub_shape = cirq.qid_shape(self.sub_gate) assert sub_shape[0] == len( self.sub_levels), ('Wrong number of levels given') sub_u = cirq.unitary(self.sub_gate).reshape(sub_shape * 2) u = cirq.eye_tensor((self.dimension, ), dtype=sub_u.dtype) temp = u[self.sub_levels, :, ...].copy() temp[:, self.sub_levels, ...] = sub_u u[self.sub_levels, :, ...] = temp return u
def test_eye_tensor(): assert np.all(cirq.eye_tensor((), dtype=int) == np.array(1)) assert np.all(cirq.eye_tensor((1,), dtype=int) == np.array([[1]])) assert np.all(cirq.eye_tensor((2,), dtype=int) == np.array([[1, 0], [0, 1]])) # yapf: disable assert np.all( cirq.eye_tensor((2, 2), dtype=int) == np.array([[[[1, 0], [0, 0]], [[0, 1], [0, 0]]], [[[0, 0], [1, 0]], [[0, 0], [0, 1]]]]) ) # yapf: disable assert np.all( cirq.eye_tensor((2, 3), dtype=int) == np.array( [ [[[1, 0, 0], [0, 0, 0]], [[0, 1, 0], [0, 0, 0]], [[0, 0, 1], [0, 0, 0]]], [[[0, 0, 0], [1, 0, 0]], [[0, 0, 0], [0, 1, 0]], [[0, 0, 0], [0, 0, 1]]], ] ) ) # yapf: disable assert np.all( cirq.eye_tensor((3, 2), dtype=int) == np.array( [ [[[1, 0], [0, 0], [0, 0]], [[0, 1], [0, 0], [0, 0]]], [[[0, 0], [1, 0], [0, 0]], [[0, 0], [0, 1], [0, 0]]], [[[0, 0], [0, 0], [1, 0]], [[0, 0], [0, 0], [0, 1]]], ] ) ) # yapf: disable
def test_single_qubit_init(): m = np.array([[1, 1j], [1j, 1]]) * np.sqrt(0.5) x2 = cirq.MatrixGate(m) assert cirq.has_unitary(x2) assert np.alltrue(cirq.unitary(x2) == m) assert cirq.qid_shape(x2) == (2, ) x2 = cirq.MatrixGate(PLUS_ONE, qid_shape=(3, )) assert cirq.has_unitary(x2) assert np.alltrue(cirq.unitary(x2) == PLUS_ONE) assert cirq.qid_shape(x2) == (3, ) with pytest.raises(ValueError, match='Not a .*unitary matrix'): cirq.MatrixGate(np.zeros((2, 2))) with pytest.raises(ValueError, match='must be a square 2d numpy array'): cirq.MatrixGate(cirq.eye_tensor((2, 2), dtype=float)) with pytest.raises(ValueError, match='must be a square 2d numpy array'): cirq.MatrixGate(np.ones((3, 4))) with pytest.raises(ValueError, match='must be a square 2d numpy array'): cirq.MatrixGate(np.ones((2, 2, 2)))
def test_apply_unitaries_mixed_qid_shapes(): class PlusOneMod3Gate(cirq.SingleQubitGate): def _qid_shape_(self): return (3, ) def _unitary_(self): return np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0]]) # yapf: disable class PlusOneMod4Gate(cirq.SingleQubitGate): def _qid_shape_(self): return (4, ) def _unitary_(self): return np.array( [[0, 0, 0, 1], [1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]] ) # yapf: disable a, b = cirq.LineQid.for_qid_shape((3, 4)) result = cirq.apply_unitaries( unitary_values=[ PlusOneMod3Gate().on(a.with_dimension(3)), cirq.X(a.with_dimension(2)), cirq.CNOT(a.with_dimension(2), b.with_dimension(2)), cirq.CNOT(a.with_dimension(2), b.with_dimension(2)), cirq.X(a.with_dimension(2)), PlusOneMod3Gate().on(a.with_dimension(3)), PlusOneMod3Gate().on(a.with_dimension(3)), ], qubits=[a, b], ) np.testing.assert_allclose(result.reshape(12), [1] + [0] * 11, atol=1e-8) result = cirq.apply_unitaries( unitary_values=[ PlusOneMod3Gate().on(a.with_dimension(3)), cirq.X(a.with_dimension(2)), cirq.CNOT(a.with_dimension(2), b.with_dimension(2)), cirq.CNOT(a.with_dimension(2), b.with_dimension(2)), cirq.X(a.with_dimension(2)), PlusOneMod3Gate().on(a.with_dimension(3)), PlusOneMod3Gate().on(a.with_dimension(3)), ], qubits=[a, b], args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((3, 4), dtype=np.complex64), available_buffer=cirq.eye_tensor((3, 4), dtype=np.complex64), axes=(0, 1), ), ) np.testing.assert_allclose(result.reshape(12, 12), np.eye(12), atol=1e-8) result = cirq.apply_unitaries( unitary_values=[ PlusOneMod3Gate().on(a.with_dimension(3)), cirq.X(a.with_dimension(2)), PlusOneMod4Gate().on(b.with_dimension(4)), PlusOneMod4Gate().on(b.with_dimension(4)), cirq.X(b.with_dimension(2)), PlusOneMod4Gate().on(b.with_dimension(4)), PlusOneMod4Gate().on(b.with_dimension(4)), cirq.CNOT(a.with_dimension(2), b.with_dimension(2)), PlusOneMod4Gate().on(b.with_dimension(4)), cirq.X(b.with_dimension(2)), cirq.CNOT(a.with_dimension(2), b.with_dimension(2)), cirq.X(a.with_dimension(2)), PlusOneMod3Gate().on(a.with_dimension(3)), PlusOneMod3Gate().on(a.with_dimension(3)), ], qubits=[a, b], args=cirq.ApplyUnitaryArgs( target_tensor=cirq.eye_tensor((3, 4), dtype=np.complex64), available_buffer=cirq.eye_tensor((3, 4), dtype=np.complex64), axes=(0, 1), ), ) np.testing.assert_allclose( result.reshape(12, 12), np.array([ [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], ]), atol=1e-8, )
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, )