Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
dev = qml.device("strawberryfields.gaussian", wires=1)
@qml.qnode(dev)
def circuit(a, phi):
qml.Displacement(a, phi, wires=0)
return qml.probs(wires=[0])
a = 0.4
phi = -0.12
n = np.arange(cutoff)
# differentiate with respect to parameter a
res_F = circuit.jacobian([a, phi], wrt={0}, method="F").flat
expected_gradient = 2 * np.exp(-a ** 2) * a ** (2 * n - 1) * (n - a ** 2) / fac(n)
assert np.allclose(res_F, expected_gradient, atol=tol, rtol=0)
# differentiate with respect to parameter phi
res_F = circuit.jacobian([a, phi], wrt={1}, method="F").flat
expected_gradient = 0
assert np.allclose(res_F, expected_gradient, atol=tol, rtol=0)
cutoff_dim = 10
dev = qml.device("strawberryfields.fock", wires=2, cutoff_dim=cutoff_dim)
sf_operation = dev._operation_map[gate_name]
assert dev.supports_operation(gate_name)
@qml.qnode(dev)
def circuit(*args):
qml.TwoModeSqueezing(0.1, 0, wires=[0, 1])
operation(*args, wires=wires)
return qml.expval(qml.NumberOperator(0)), qml.expval(qml.NumberOperator(1))
res = circuit(V, r)
sf_res = SF_gate_reference(sf_operation, cutoff_dim, wires, V, r)
assert np.allclose(res, sf_res, atol=tol, rtol=0)
def test_basis_state_2_qubit_subset(self, qubit_device_2_wires, tol, name, par, wires, expected_output):
"""Tests qubit basis state preparation on subsets of qubits"""
op = getattr(qml.ops, name)
@qml.qnode(qubit_device_2_wires)
def circuit():
op(np.array(par), wires=wires)
return qml.expval(qml.PauliZ(0)), qml.expval(qml.PauliZ(1))
assert np.allclose(circuit(), expected_output, atol=tol, rtol=0)
cutoff_dim = 10
dev = qml.device("strawberryfields.fock", wires=2, cutoff_dim=cutoff_dim)
sf_operation = dev._operation_map[gate_name]
assert dev.supports_operation(gate_name)
@qml.qnode(dev)
def circuit(*args):
qml.TwoModeSqueezing(0.1, 0, wires=[0, 1])
operation(*args, wires=wires)
return qml.expval(qml.NumberOperator(0)), qml.expval(qml.NumberOperator(1))
res = circuit(a, b, c)
sf_res = SF_gate_reference(sf_operation, cutoff_dim, wires, a * np.exp(1j * b), c)
assert np.allclose(res, sf_res, atol=tol, rtol=0)
dev = qml.device("strawberryfields.gaussian", wires=2)
sf_operation = dev._operation_map[gate_name]
assert dev.supports_operation(gate_name)
@qml.qnode(dev)
def circuit(*args):
qml.TwoModeSqueezing(0.1, 0, wires=[0, 1])
operation(*args, wires=wires)
return qml.expval(qml.NumberOperator(0)), qml.expval(qml.NumberOperator(1))
res = circuit(U)
sf_res = SF_gate_reference(sf_operation, wires, U)
assert np.allclose(res, sf_res, atol=tol, rtol=0)
def test_two_mode_squeezing_heisenberg(phi, mag):
"""ops: Tests the Heisenberg representation of the Beamsplitter gate."""
r = mag
matrix = cv.TwoModeSqueezing._heisenberg_rep([r, phi])
true_matrix = np.array(
[
[1, 0, 0, 0, 0],
[0, np.cosh(r), 0, np.cos(phi) * np.sinh(r), np.sin(phi) * np.sinh(r)],
[0, 0, np.cosh(r), np.sin(phi) * np.sinh(r), -np.cos(phi) * np.sinh(r)],
[0, np.cos(phi) * np.sinh(r), np.sin(phi) * np.sinh(r), np.cosh(r), 0],
[0, np.sin(phi) * np.sinh(r), -np.cos(phi) * np.sinh(r), 0, np.cosh(r)],
]
)
assert np.allclose(matrix, true_matrix)
def test_squeezing_heisenberg(phi, mag):
"""ops: Tests the Heisenberg representation of the Squeezing gate."""
r = mag
matrix = cv.Squeezing._heisenberg_rep([r, phi])
true_matrix = np.array(
[
[1, 0, 0],
[0, np.cosh(r) - np.cos(phi) * np.sinh(r), -np.sin(phi) * np.sinh(r)],
[0, -np.sin(phi) * np.sinh(r), np.cosh(r) + np.cos(phi) * np.sinh(r)],
]
)
assert np.allclose(matrix, true_matrix)
def test_pauliz_hadamard(self, theta, phi, varphi, monkeypatch, tol):
"""Test that a tensor product involving PauliZ and PauliY and hadamard works correctly"""
dev = qml.device("default.qubit", wires=3)
dev.reset()
dev.apply("RX", wires=[0], par=[theta])
dev.apply("RX", wires=[1], par=[phi])
dev.apply("RX", wires=[2], par=[varphi])
dev.apply("CNOT", wires=[0, 1], par=[])
dev.apply("CNOT", wires=[1, 2], par=[])
with monkeypatch.context() as m:
m.setattr("numpy.random.choice", lambda x, y, p: (x, p))
s1, p = dev.sample(["PauliZ", "Hadamard", "PauliY"], [[0], [1], [2]], [[], [], []])
# s1 should only contain 1 and -1
assert np.allclose(s1 ** 2, 1, atol=tol, rtol=0)
mean = s1 @ p
expected = -(np.cos(varphi) * np.sin(phi) + np.sin(varphi) * np.cos(theta)) / np.sqrt(2)
assert np.allclose(mean, expected, atol=tol, rtol=0)
var = (s1 ** 2) @ p - (s1 @ p).real ** 2
expected = (
3
+ np.cos(2 * phi) * np.cos(varphi) ** 2
- np.cos(2 * theta) * np.sin(varphi) ** 2
- 2 * np.cos(theta) * np.sin(phi) * np.sin(2 * varphi)
) / 4
assert np.allclose(var, expected, atol=tol, rtol=0)
qml.Displacement(a, phi, wires=1)
return qml.probs(wires=[0, 1])
a = 0.4
phi = -0.12
c = np.arange(cutoff)
d = np.arange(cutoff)
n0, n1 = np.meshgrid(c, d)
n0 = n0.flatten()
n1 = n1.flatten()
# differentiate with respect to parameter a
res_F = circuit.jacobian([a, phi], wrt={0}, method="F").flat
expected_gradient = 2 * (a **(-1 + 2*n0 + 2*n1)) * np.exp(-2*a ** 2) * (-2*a ** 2 + n0 + n1) / (fac(n0) * fac(n1))
assert np.allclose(res_F, expected_gradient, atol=tol, rtol=0)
# differentiate with respect to parameter phi
res_F = circuit.jacobian([a, phi], wrt={1}, method="F").flat
expected_gradient = 0
assert np.allclose(res_F, expected_gradient, atol=tol, rtol=0)
r = 0.4
phi = -0.12
n = np.arange(cutoff)
# differentiate with respect to parameter r
res_F = circuit.jacobian([r, phi], wrt={0}, method="F").flatten()
assert res_F.shape == (cutoff,)
expected_gradient = (
np.abs(np.tanh(r)) ** n * (1 + 2 * n - np.cosh(2 * r)) * fac(n)
/ (2 ** (n + 1) * np.cosh(r) **2 * np.sinh(r) * fac(n / 2) ** 2)
)
expected_gradient[n % 2 != 0] = 0
assert np.allclose(res_F, expected_gradient, atol=tol, rtol=0)
# differentiate with respect to parameter phi
res_F = circuit.jacobian([r, phi], wrt={1}, method="F").flat
expected_gradient = 0
assert np.allclose(res_F, expected_gradient, atol=tol, rtol=0)