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_eval_true_state_fock_prob(self, setup_eng, cutoff, tol):
"""Tests whether the probability of a Fock measurement outcome on the state returns
the correct value when eval=True is passed to the fock_prob method of a state."""
n1 = cutoff // 2
n2 = cutoff // 3
eng, prog = setup_eng(2)
with prog.context as q:
Dgate(ALPHA) | q[0]
Dgate(-ALPHA) | q[1]
state = eng.run(prog).state
prob = state.fock_prob([n1, n2], eval=True)
ref_prob = np.abs(
np.outer(coherent_state(ALPHA, cutoff), coherent_state(-ALPHA, cutoff)) ** 2
)[n1, n2]
assert np.allclose(prob, ref_prob, atol=tol, rtol=0.0)
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()
assert np.allclose(cv, cv1)
assert np.allclose(mean, mean1)
def test_subsystems(self, setup_eng, tol):
"""Check that the backend keeps in sync with the program when creating and deleting modes."""
null = sf.Program(2) # empty program
eng, prog = setup_eng(2)
# 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 test_eval_false_state_fidelity(self, setup_eng, cutoff):
"""Tests whether the fidelity of the state with respect to a local state is
an unevaluated Tensor object when eval=False is passed to the fidelity method of a state."""
eng, prog = setup_eng(1)
with prog.context as q:
Dgate(ALPHA) | q
state = eng.run(prog).state
fidel = state.fidelity(coherent_state(ALPHA, cutoff), 0, eval=False)
assert isinstance(fidel, tf.Tensor)
def test_eng_run_eval_false_state_fidelity(self, setup_eng, cutoff):
"""Tests whether the fidelity of the state with respect to a local state is an
unevaluated Tensor object when eval=False is passed to `eng.run`."""
eng, prog = setup_eng(1)
with prog.context as q:
Dgate(ALPHA) | q
state = eng.run(prog, run_options=evalf).state
fidel = state.fidelity(coherent_state(ALPHA, cutoff), 0)
assert isinstance(fidel, tf.Tensor)
def single_input_circuit(x):
eng.reset()
with eng:
Dgate(x[0], 0.) | q[0]
Dgate(x[1], 0.) | q[1]
BSgate(phi=params[0]) | (q[0], q[1])
BSgate() | (q[0], q[1])
state = eng.run('fock', cutoff_dim=10, eval=True)
p0 = state.fock_prob([0, 2])
p1 = state.fock_prob([2, 0])
normalization = p0 + p1 + 1e-10
output = p1 / normalization
return output
def circuit(params):
eng, q = sf.Engine(1)
with eng:
Dgate(params[0]) | q[0]
state = eng.run('fock', cutoff_dim=7)
circuit_output = state.fock_prob([1])
trace = state.trace()
# Log the trace of the state to check if it is 1
log = {'Prob': circuit_output,
'Trace': trace}
# The second return value can be an optional log dictionary
# of one or more values
return circuit_output, log
Returns:
tuple: a tuple containing the output fidelity to the target ON state,
the probability of post-selection, the state norm before entering the beamsplitter,
the state norm after exiting the beamsplitter, and the density matrix of the output state.
"""
# define target state
ONdm = on_state(a, cutoff)
# unpack circuit parameters
sq0_r, sq0_phi, disp0_r, disp0_phi, sq1_r, sq1_phi, disp1_r, disp1_phi, theta, phi = params
# quantum circuit prior to entering the beamsplitter
prog1 = sf.Program(2)
with prog1.context as q1:
Sgate(sq0_r, sq0_phi) | q1[0]
Dgate(disp0_r, disp0_phi) | q1[0]
Sgate(sq1_r, sq1_phi) | q1[1]
Dgate(disp1_r, disp1_phi) | q1[1]
eng = sf.Engine("fock", backend_options={"cutoff_dim": cutoff})
stateIn = eng.run(prog1).state
normIn = np.abs(stateIn.trace())
# norm of output state and probability
prog_BS = sf.Program(2)
with prog_BS.context as q1:
BSgate(theta, phi) | (q1[0], q1[1])
stateOut = eng.run(prog_BS).state
normOut = np.abs(stateOut.trace())
rho = stateOut.dm()
for i in range(mode_number):
Rgate(phase_variables[layer_number, i, 0]) | q[i]
for i in range(mode_number):
Sgate(tf.clip_by_value(sq_magnitude_variables[layer_number, i], -sq_clip, sq_clip),
sq_phase_variables[layer_number, i]) | q[i]
BSgate(bs_variables[layer_number, 0, 1, 0], bs_variables[layer_number, 0, 1, 1]) \
| (q[0], q[1])
for i in range(mode_number):
Rgate(phase_variables[layer_number, i, 1]) | q[i]
for i in range(mode_number):
Dgate(tf.clip_by_value(disp_magnitude_variables[layer_number, i], -disp_clip,
disp_clip), disp_phase_variables[layer_number, i]) | q[i]
for i in range(mode_number):
Kgate(tf.clip_by_value(kerr_variables[layer_number, i], -kerr_clip, kerr_clip)) | q[i]