Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_loss_channel(self, setup_eng, tol):
"""Test loss channel with no transmission produces vacuum"""
eng, prog = setup_eng(1)
with prog.context as q:
ops.Dgate(A) | q[0]
ops.LossChannel(0) | q[0]
state = eng.run(prog)
assert np.all(eng.backend.is_vacuum(tol))
def test_ket_input_validation(self, setup_eng, hbar, cutoff):
"""Test exceptions"""
mu = np.array([0.0, 0.0])
cov = np.identity(2)
state1 = GaussianState((mu, cov), 1, None, None)
state2 = BaseFockState(np.zeros(cutoff), 1, False, cutoff)
eng, prog = setup_eng(2)
with prog.context as q:
with pytest.raises(ValueError, match="Gaussian states are not supported"):
ops.Ket(state1) | q[0]
with pytest.raises(ValueError, match="not pure"):
ops.Ket(state2) | q[0]
def unitary(q):
ops.MZgate(0.5, 0.1) | (q[0], q[1])
ops.BSgate(0.1, 0.2) | (q[1], q[2])
ops.Rgate(0.4) | q[0]
def test_embed_graph(self, setup_eng, hbar):
"""Test that an embedded graph has the right total mean photon number. """
eng, prog = setup_eng(2)
A = np.array([[0.0, 1.0], [1.0, 0.0]])
n_mean_per_mode = 1
with prog.context as q:
ops.GraphEmbed(A, n_mean_per_mode) | (q[0], q[1])
state = eng.run(prog).state
cov = state.cov()
n_mean_tot = np.trace(cov / (hbar / 2) - np.identity(4)) / 4
expected = 2 * n_mean_per_mode
assert np.allclose(n_mean_tot, expected)
def test_homodyne_measurement_vacuum(self, setup_eng, tol):
"""MeasureX and MeasureP leave the mode in the vacuum state"""
eng, prog = setup_eng(2)
with prog.context as q:
ops.Coherent(a, c) | q[0]
ops.Coherent(b, c) | q[1]
ops.MeasureX | q[0]
ops.MeasureP | q[1]
eng.run(prog)
assert np.all(eng.backend.is_vacuum(tol))
# define some gates
D = ops.Dgate(0.5)
BS = ops.BSgate(2 * np.pi, np.pi / 2)
R = ops.Rgate(np.pi)
with prog.context as q:
alice, bob = q
D | alice
BS | (alice, bob)
ops.Del | alice
R | bob
charlie, = ops.New(1)
BS | (bob, charlie)
ops.MeasureX | bob
ops.Del | bob
D.H | charlie
ops.MeasureX | charlie
def check_reg(p, expected_n=None):
"""Compare Program.register with the mode list returned by the backend.
They should always be in agreement after Engine.run() and Engine.reset().
"""
rr = p.register
modes = eng.backend.get_modes()
# number of elements
assert len(rr) == len(modes)
if expected_n is not None:
assert len(rr) == expected_n
# check indices match
def test_gate_noarg(self):
"""Test gate with no argument converts"""
# create a test program
prog = Program(1)
with prog.context as q:
ops.Vac | q[0]
bb = io.to_blackbird(prog)
expected = {"op": "Vacuum", "modes": [0], "args": [], "kwargs": {}}
assert bb.operations[0] == expected
def test_GBS_compile_no_fock_meas(self):
"""Tests that GBS compilation fails when no fock measurements are made."""
prog = sf.Program(2)
with prog.context as q:
ops.Dgate(1.0) | q[0]
ops.Sgate(-0.5) | q[1]
with pytest.raises(program.CircuitError, match="GBS circuits must contain Fock measurements."):
prog.compile('gbs')
def test_Pgate(self, setup_eng, pure, hbar, tol):
"""Test the action of the P gate in phase space"""
if not pure:
pytest.skip("Test only runs on pure states")
N = 1
eng, prog = setup_eng(N)
r = 3
x1 = 2
p1 = 1.3
s = 0.5
with prog.context as q:
ops.Sgate(r) | q
ops.Xgate(x1) | q
ops.Zgate(p1) | q
ops.Pgate(s) | q
state = eng.run(prog).state
Pmat = np.array([[1, 0], [s, 1]])
Vexpected = 0.5 * hbar * Pmat @ np.diag(np.exp([-2 * r, 2 * r])) @ Pmat.T
assert np.allclose(Vexpected, state.cov(), atol=tol, rtol=0)
rexpected = Pmat @ np.array([x1, p1])
assert np.allclose(rexpected, state.means(), atol=tol, rtol=0)
raise ValueError("Loss parameter must take a value between zero and one")
modes = graph.order()
photons = sum(orbit)
A = nx.to_numpy_array(graph)
mean_photon_per_mode = n_mean / float(modes)
p = sf.Program(modes)
# pylint: disable=expression-not-assigned
with p.context as q:
sf.ops.GraphEmbed(A, mean_photon_per_mode=mean_photon_per_mode) | q
if loss:
for _q in q:
sf.ops.LossChannel(1 - loss) | _q
eng = sf.LocalEngine(backend="gaussian")
result = eng.run(p)
prob = 0
for _ in range(samples):
sample = orbit_to_sample(orbit, modes)
prob += result.state.fock_prob(sample, cutoff=photons + 1)
prob = prob * orbit_cardinality(orbit, modes) / samples
return prob