def test_has_mixture(): assert cirq.has_mixture(ReturnsValidTuple()) assert not cirq.has_mixture(ReturnsNotImplemented()) assert cirq.has_mixture(ReturnsMixtureButNoHasMixture()) assert cirq.has_mixture(ReturnsUnitary()) assert not cirq.has_mixture(ReturnsNotImplementedUnitary()) class NoAtom(cirq.Operation): @property def qubits(self): return cirq.LineQubit.range(2) def with_qubits(self): raise NotImplementedError() class No1: def _decompose_(self): return [NoAtom()] class Yes1: def _decompose_(self): return [cirq.X(cirq.LineQubit(0))] with cirq.testing.assert_logs('cirq.has_mixture', ' has_mixture_channel '): assert not cirq.has_mixture_channel(No1()) with cirq.testing.assert_logs('cirq.has_mixture', ' has_mixture_channel '): assert cirq.has_mixture_channel(Yes1())
def test_phase_damping_channel(): d = cirq.phase_damp(0.3) np.testing.assert_almost_equal(cirq.channel(d), (np.array([[1.0, 0.], [0., np.sqrt(1 - 0.3)]]), np.array([[0., 0.], [0., np.sqrt(0.3)]]))) assert cirq.has_channel(d) assert not cirq.has_mixture_channel(d)
def test_asymmetric_depolarizing_mixture(): d = cirq.asymmetric_depolarize(0.1, 0.2, 0.3) assert_mixtures_equal(cirq.mixture(d), ((0.4, np.eye(2)), (0.1, X), (0.2, Y), (0.3, Z))) assert cirq.has_mixture_channel(d)
def test_depolarizing_mixture(): d = cirq.depolarize(0.3) assert_mixtures_equal(cirq.mixture(d), ((0.7, np.eye(2)), (0.1, X), (0.1, Y), (0.1, Z))) assert cirq.has_mixture_channel(d)
def test_reset_channel(): r = cirq.reset(cirq.LineQubit(0)) np.testing.assert_almost_equal( cirq.channel(r), (np.array([[1., 0.], [0., 0]]), np.array([[0., 1.], [0., 0.]]))) assert cirq.has_channel(r) assert not cirq.has_mixture_channel(r) assert cirq.qid_shape(r) == (2,) r = cirq.reset(cirq.LineQid(0, dimension=3)) np.testing.assert_almost_equal( cirq.channel(r), (np.array([[1, 0, 0], [0, 0, 0], [0, 0, 0]]), np.array([[0, 1, 0], [0, 0, 0], [0, 0, 0]]), np.array([[0, 0, 1], [0, 0, 0], [0, 0, 0]]))) # yapf: disable assert cirq.has_channel(r) assert not cirq.has_mixture_channel(r) assert cirq.qid_shape(r) == (3,)
def test_generalized_amplitude_damping_channel(): d = cirq.generalized_amplitude_damp(0.1, 0.3) np.testing.assert_almost_equal(cirq.channel(d), (np.sqrt(0.1) * np.array([[1., 0.], [0., np.sqrt(1.-0.3)]]), np.sqrt(0.1) * np.array([[0., np.sqrt(0.3)], [0., 0.]]), np.sqrt(0.9) * np.array([[np.sqrt(1. - 0.3), 0.], [0., 1.]]), np.sqrt(0.9) * np.array([[0., 0.], [np.sqrt(0.3), 0.]]))) assert cirq.has_channel(d) assert not cirq.has_mixture_channel(d)
def test_deprecated_mixture_channel(): with cirq.testing.assert_logs('"cirq.mixture"', ' mixture_channel '): _ = cirq.mixture_channel(cirq.X) with cirq.testing.assert_logs('"cirq.has_mixture"', ' has_mixture_channel '): _ = cirq.has_mixture_channel(cirq.X)
def test_bit_flip_mixture(): d = cirq.bit_flip(0.3) assert_mixtures_equal(cirq.mixture(d), ((0.7, np.eye(2)), (0.3, X))) assert cirq.has_mixture_channel(d)
def test_reset_channel(): np.testing.assert_almost_equal( cirq.channel(cirq.RESET), (np.array([[1., 0.], [0., 0]]), np.array([[0., 1.], [0., 0.]]))) assert cirq.has_channel(cirq.RESET) assert not cirq.has_mixture_channel(cirq.RESET)
def test_phase_flip_mixture(): d = cirq.phase_flip(0.3) assert_mixtures_equal(cirq.mixture(d), ((0.3, np.eye(2)), (0.7, Z))) assert cirq.has_mixture_channel(d)
def test_has_mixture_channel(): assert cirq.has_mixture_channel(ReturnsValidTuple()) assert not cirq.has_mixture_channel(ReturnsNotImplemented()) assert cirq.has_mixture_channel(ReturnsMixtureButNoHasMixture()) assert cirq.has_mixture_channel(ReturnsUnitary()) assert not cirq.has_mixture_channel(ReturnsNotImplementedUnitary())