Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def circuit(x):
qml.RX(x, wires=0)
return qml.expval(qml.PauliY(0))
def circuit(x, y, z):
qml.CNOT(wires=[0, 1])
return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliY(1))
def test_construct(self):
"""Test construction of a tensor product"""
X = qml.PauliX(0)
Y = qml.PauliY(2)
T = Tensor(X, Y)
assert T.obs == [X, Y]
T = Tensor(T, Y)
assert T.obs == [X, Y, Y]
with pytest.raises(ValueError, match="Can only perform tensor products between observables"):
Tensor(T, qml.CNOT(wires=[0, 1]))
(qml.PauliY(wires=[0]).inv(), [cirq.Y ** -1]),
(qml.PauliZ(wires=[0]).inv(), [cirq.Z ** -1]),
(qml.Hadamard(wires=[0]), [cirq.H]),
(qml.Hadamard(wires=[0]).inv(), [cirq.H ** -1]),
(qml.S(wires=[0]), [cirq.S]),
(qml.S(wires=[0]).inv(), [cirq.S ** -1]),
(qml.PhaseShift(1.4, wires=[0]), [cirq.ZPowGate(exponent=1.4 / np.pi)]),
(qml.PhaseShift(-1.2, wires=[0]), [cirq.ZPowGate(exponent=-1.2 / np.pi)]),
(qml.PhaseShift(2, wires=[0]), [cirq.ZPowGate(exponent=2 / np.pi)]),
(
qml.PhaseShift(1.4, wires=[0]).inv(),
[cirq.ZPowGate(exponent=-1.4 / np.pi)],
),
(
qml.PhaseShift(-1.2, wires=[0]).inv(),
[cirq.ZPowGate(exponent=1.2 / np.pi)],
),
def test_parameters_available_at_pre_measure(self, mock_device, monkeypatch):
"""Tests that the parameter mapping is available when pre_measure is called and that accessing
Device.parameters raises no error"""
p0 = 0.54
p1 = -0.32
queue = [
qml.RX(p0, wires=0),
qml.PauliY(wires=1),
qml.Rot(0.432, 0.123, p1, wires=2),
]
parameters = {0: (0, 0), 1: (2, 3)}
observables = [
qml.expval(qml.PauliZ(0)),
qml.var(qml.PauliZ(1)),
qml.sample(qml.PauliZ(2)),
]
p_mapping = {}
with monkeypatch.context() as m:
m.setattr(Device, "pre_measure", lambda self: p_mapping.update(self.parameters))
mock_device.execute(queue, observables, parameters=parameters)
def test_pauliy_expectation(self, device, shots, tol):
"""Test that PauliY expectation value is correct"""
theta = 0.432
phi = 0.123
dev = device(2)
O = qml.PauliY
with mimic_execution_for_expval(dev):
dev.apply(
[qml.RX(theta, wires=[0]), qml.RX(phi, wires=[1]), qml.CNOT(wires=[0, 1]),],
rotations=O(wires=[0], do_queue=False).diagonalizing_gates()
+ O(wires=[1], do_queue=False).diagonalizing_gates(),
)
dev._obs_queue = [O(wires=[0], do_queue=False), O(wires=[1], do_queue=False)]
res = np.array(
[dev.expval(O(wires=[0], do_queue=False)), dev.expval(O(wires=[1], do_queue=False)),]
)
assert np.allclose(res, np.array([0, -(np.cos(theta)) * np.sin(phi)]), **tol)
def test_eigvals(self):
"""Test that the correct eigenvalues are returned for the Tensor"""
X = qml.PauliX(0)
Y = qml.PauliY(2)
t = Tensor(X, Y)
assert np.array_equal(t.eigvals, np.kron(qml.PauliX.eigvals, qml.PauliY.eigvals))
def layer3_diag(x, y, z, h, g, f):
non_parametrized_layer(a, b, c)
qml.RX(x, wires=0)
qml.RY(y, wires=1)
qml.RZ(z, wires=2)
non_parametrized_layer(a, b, c)
return qml.var(qml.PauliZ(2)), qml.var(qml.PauliY(1))