class TestKrausOps: """Unit tests for the method `_get_kraus_ops()`""" unitary_ops = [(qml.PauliX, np.array([[0, 1], [1, 0]])), (qml.Hadamard, np.array([[INV_SQRT2, INV_SQRT2], [INV_SQRT2, -INV_SQRT2]])), (qml.CNOT, np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]]))] @pytest.mark.parametrize("ops", unitary_ops) def test_unitary_kraus(self, ops): """Tests that matrices of non-diagonal unitary operations are retrieved correctly""" dev = qml.device('default.mixed', wires=2) assert np.allclose(dev._get_kraus(ops[0]), [ops[1]]) diagonal_ops = [(qml.PauliZ(wires=0), np.array([1, -1])), (qml.CZ(wires=[0, 1]), np.array([1, 1, 1, -1]))] @pytest.mark.parametrize("ops", diagonal_ops) def test_diagonal_kraus(self, ops): """Tests that matrices of non-diagonal unitary operations are retrieved correctly""" dev = qml.device('default.mixed', wires=2) assert np.allclose(dev._get_kraus(ops[0]), ops[1]) p = 0.5 K0 = np.sqrt(1 - p) * np.eye(2) K1 = np.sqrt(p / 3) * np.array([[0, 1], [1, 0]]) K2 = np.sqrt(p / 3) * np.array([[0, -1j], [1j, 0]]) K3 = np.sqrt(p / 3) * np.array([[1, 0], [0, -1]]) channel_ops = [(qml.AmplitudeDamping(p, wires=0), [ np.diag([1, np.sqrt(1 - p)]), np.sqrt(p) * np.array([[0, 1], [0, 0]]) ]), (qml.DepolarizingChannel(p, wires=0), [K0, K1, K2, K3])] @pytest.mark.parametrize("ops", channel_ops) def test_channel_kraus(self, ops): """Tests that krause matrices of non-unitary channels are retrieved correctly""" dev = qml.device('default.mixed', wires=1) assert np.allclose(dev._get_kraus(ops[0]), ops[1])
def circuit(p): qml.RX(angle, wires=0) qml.DepolarizingChannel(p, wires=0) return qml.expval(qml.PauliZ(0))
def depolarizing_circuit(p): qml.Hadamard(wires=0) qml.CNOT(wires=[0, 1]) qml.DepolarizingChannel(p, wires=0) qml.DepolarizingChannel(p, wires=1) return qml.expval(qml.PauliZ(0) @ qml.PauliZ(1))
def noisy_operations(damp_factor, depo_factor, flip_prob): qml.AmplitudeDamping(damp_factor, wires=0) qml.DepolarizingChannel(depo_factor, wires=0) qml.BitFlip(flip_prob, wires=0)