def test_start(self): """Test if the expected tape is returned when the start position is requested""" tape = insert(qml.AmplitudeDamping, 0.4, position="start")(self.tape) with QuantumTape() as tape_exp: qml.AmplitudeDamping(0.4, wires=0) qml.AmplitudeDamping(0.4, wires=1) qml.RX(0.9, wires=0) qml.RY(0.4, wires=1) qml.CNOT(wires=[0, 1]) qml.RY(0.5, wires=0) qml.RX(0.6, wires=1) qml.expval(qml.PauliZ(0) @ qml.PauliZ(1)) assert all(o1.name == o2.name for o1, o2 in zip(tape.operations, tape_exp.operations)) assert all(o1.wires == o2.wires for o1, o2 in zip(tape.operations, tape_exp.operations)) assert all( np.allclose(o1.parameters, o2.parameters) for o1, o2 in zip(tape.operations, tape_exp.operations)) assert len(tape.measurements) == 1 assert tape.observables[0].name == ["PauliZ", "PauliZ"] assert tape.observables[0].wires.tolist() == [0, 1] assert tape.measurements[0].return_type is Expectation
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 damping_circuit(x): qml.Hadamard(wires=0) qml.CNOT(wires=[0, 1]) qml.AmplitudeDamping(sigmoid(x), wires=0) # p = sigmoid(x) qml.AmplitudeDamping(sigmoid(x), 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)