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_non_primitive_gates():
"""Tests that the compiler is able to compile a number of non-primitive Gaussian gates"""
width = 6
eng = sf.LocalEngine(backend="gaussian")
eng1 = sf.LocalEngine(backend="gaussian")
circuit = sf.Program(width)
A = np.random.rand(width, width) + 1j * np.random.rand(width, width)
A = A + A.T
valsA = np.linalg.svd(A, compute_uv=False)
A = A / 2 * np.max(valsA)
B = np.random.rand(width // 2, width // 2) + 1j * np.random.rand(width // 2, width // 2)
valsB = np.linalg.svd(B, compute_uv=False)
B = B / 2 * valsB
B = np.block([[0 * B, B], [B.T, 0 * B]])
with circuit.context as q:
ops.GraphEmbed(A) | q
ops.BipartiteGraphEmbed(B) | q
ops.Pgate(0.1) | q[1]
ops.CXgate(0.2) | (q[0], q[1])
ops.MZgate(0.4, 0.5) | (q[2], q[3])
def test_incorrect_python_version(self, monkeypatch):
"""Test that an exception is raised if the version
of Python installed is > 3.6"""
with monkeypatch.context() as m:
m.setattr("sys.version_info", (3, 8, 1))
m.setattr(tensorflow, "__version__", "1.12.2")
with pytest.raises(ImportError, match="you will need to install Python 3.6"):
reload(sf.backends.tfbackend)
sf.LocalEngine('tf')
def eng(backend):
"""Engine fixture."""
return sf.LocalEngine(backend)
def test_gaussian_program(depth, width):
"""Tests that a circuit and its compiled version produce the same Gaussian state"""
eng = sf.LocalEngine(backend="gaussian")
eng1 = sf.LocalEngine(backend="gaussian")
circuit = sf.Program(width)
with circuit.context as q:
for _ in range(depth):
U, s, V, alphas = random_params(width, 2.0 / depth, 1.0)
ops.Interferometer(U) | q
for i in range(width):
ops.Sgate(s[i]) | q[i]
ops.Interferometer(V) | q
for i in range(width):
ops.Dgate(alphas[i]) | q[i]
compiled_circuit = circuit.compile("gaussian_unitary")
cv = eng.run(circuit).state.cov()
mean = eng.run(circuit).state.means()
cv1 = eng1.run(compiled_circuit).state.cov()
def test_displacements_only(depth, width):
"""Tests that a circuit and its compiled version produce
the same Gaussian state when there are only displacements"""
eng = sf.LocalEngine(backend="gaussian")
eng1 = sf.LocalEngine(backend="gaussian")
circuit = sf.Program(width)
with circuit.context as q:
for _ in range(depth):
alphas = np.random.rand(width)+1j*np.random.rand(width)
for i in range(width):
ops.Dgate(alphas[i]) | q[i]
compiled_circuit = circuit.compile("gaussian_unitary")
cv = eng.run(circuit).state.cov()
mean = eng.run(circuit).state.means()
cv1 = eng1.run(compiled_circuit).state.cov()
mean1 = eng1.run(compiled_circuit).state.means()
assert np.allclose(cv, cv1)
assert np.allclose(mean, mean1)
def eng(backend):
"""Engine fixture."""
return sf.LocalEngine(backend)
def test_bad_backend(self):
"""Backend must be a string or a BaseBackend instance."""
with pytest.raises(TypeError, match='backend must be a string or a BaseBackend instance'):
eng = sf.LocalEngine(0)
def test_all_loss(self, monkeypatch):
"""Test if function samples from the vacuum when maximum loss is applied."""
dim = 5
graph = nx.complete_graph(dim)
mock_eng_run = mock.MagicMock()
with monkeypatch.context() as m:
m.setattr(sf.LocalEngine, "run", mock_eng_run)
similarity.prob_orbit_mc(graph, [1, 1, 1, 1], samples=1, loss=1)
p_func = mock_eng_run.call_args[0][0]
eng = sf.LocalEngine(backend="gaussian")
state = eng.run(p_func).state
cov = state.cov()
disp = state.displacement()
assert np.allclose(cov, 0.5 * state.hbar * np.eye(2 * dim))
assert np.allclose(disp, np.zeros(dim))
def test_gaussian_program(depth, width):
"""Tests that a circuit and its compiled version produce the same Gaussian state"""
eng = sf.LocalEngine(backend="gaussian")
eng1 = sf.LocalEngine(backend="gaussian")
circuit = sf.Program(width)
with circuit.context as q:
for _ in range(depth):
U, s, V, alphas = random_params(width, 2.0 / depth, 1.0)
ops.Interferometer(U) | q
for i in range(width):
ops.Sgate(s[i]) | q[i]
ops.Interferometer(V) | q
for i in range(width):
ops.Dgate(alphas[i]) | q[i]
compiled_circuit = circuit.compile("gaussian_unitary")
cv = eng.run(circuit).state.cov()
mean = eng.run(circuit).state.means()
cv1 = eng1.run(compiled_circuit).state.cov()
mean1 = eng1.run(compiled_circuit).state.means()