Skip to content

Commit

Permalink
Merge branch 'master' into qcut_integ_tests_4_validation
Browse files Browse the repository at this point in the history
  • Loading branch information
trbromley committed Mar 1, 2022
2 parents 137f4a4 + 9c344f5 commit 4a13302
Showing 1 changed file with 0 additions and 102 deletions.
102 changes: 0 additions & 102 deletions tests/transforms/test_qcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -2003,108 +2003,6 @@ def circuit(x):

assert np.isclose(gradient, cut_gradient)

def test_simple_cut_circuit_torch(self, use_opt_einsum):
"""
Tests the full circuit cutting pipeline returns the correct value and
gradient for a simple circuit using the `cut_circuit` transform with the torch interface.
"""
torch = pytest.importorskip("torch")

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev, interface="torch")
def circuit(x):
qml.RX(x, wires=0)
qml.RY(0.543, wires=1)
qml.WireCut(wires=0)
qml.CNOT(wires=[0, 1])
qml.RZ(0.240, wires=0)
qml.RZ(0.133, wires=1)
return qml.expval(qml.PauliZ(wires=[0]))

x = torch.tensor(0.531, requires_grad=True)
cut_circuit = qcut.cut_circuit(circuit, use_opt_einsum=use_opt_einsum)

res = cut_circuit(x)
res_expected = circuit(x)
assert np.isclose(res.detach().numpy(), res_expected.detach().numpy())

res.backward()
grad = x.grad.detach().numpy()

x.grad = None
res_expected.backward()
grad_expected = x.grad.detach().numpy()

assert np.isclose(grad, grad_expected)

def test_simple_cut_circuit_tf(self, use_opt_einsum):
"""
Tests the full circuit cutting pipeline returns the correct value and
gradient for a simple circuit using the `cut_circuit` transform with the TF interface.
"""
tf = pytest.importorskip("tensorflow")

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev, interface="tf")
def circuit(x):
qml.RX(x, wires=0)
qml.RY(0.543, wires=1)
qml.WireCut(wires=0)
qml.CNOT(wires=[0, 1])
qml.RZ(0.240, wires=0)
qml.RZ(0.133, wires=1)
return qml.expval(qml.PauliZ(wires=[0]))

x = tf.Variable(0.531)
cut_circuit = qcut.cut_circuit(circuit, use_opt_einsum=use_opt_einsum)

with tf.GradientTape() as tape:
res = cut_circuit(x)

grad = tape.gradient(res, x)

with tf.GradientTape() as tape:
res_expected = circuit(x)

grad_expected = tape.gradient(res_expected, x)

assert np.isclose(res, res_expected)
assert np.isclose(grad, grad_expected)

def test_simple_cut_circuit_jax(self, use_opt_einsum):
"""
Tests the full circuit cutting pipeline returns the correct value and
gradient for a simple circuit using the `cut_circuit` transform with the Jax interface.
"""
jax = pytest.importorskip("jax")
import jax.numpy as jnp

dev = qml.device("default.qubit", wires=2)

@qml.qnode(dev, interface="jax")
def circuit(x):
qml.RX(x, wires=0)
qml.RY(0.543, wires=1)
qml.WireCut(wires=0)
qml.CNOT(wires=[0, 1])
qml.RZ(0.240, wires=0)
qml.RZ(0.133, wires=1)
return qml.expval(qml.PauliZ(wires=[0]))

x = jnp.array(0.531)
cut_circuit = qcut.cut_circuit(circuit, use_opt_einsum=use_opt_einsum)

res = cut_circuit(x)
res_expected = circuit(x)

grad = jax.grad(cut_circuit)(x)
grad_expected = jax.grad(circuit)(x)

assert np.isclose(res, res_expected)
assert np.isclose(grad, grad_expected)


class TestCutCircuitTransformValidation:
"""Tests of validation checks in the cut_circuit function"""
Expand Down

0 comments on commit 4a13302

Please sign in to comment.