Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setup_one_mode_circuit(self, setup_eng, cutoff):
"""Create the circuit for following tests"""
eng_ref, p0 = setup_eng(1)
S = ops.Sgate(1.1, -1.4)
L = ops.LossChannel(0.45)
initial_state = np.random.rand(cutoff, cutoff)
with p0.context as q:
ops.DensityMatrix(initial_state) | q
prog = sf.Program(p0)
with prog.context as q:
S | q
L | q
rho = eng_ref.run([p0, prog]).state.dm()
return prog, rho, initial_state
def test_S2gate_decomp_equal(self, setup_eng, r, tol):
"""Tests that the S2gate gives the same transformation as its decomposition."""
eng, prog = setup_eng(2)
phi = 0.273
BS = ops.BSgate(np.pi / 4, 0)
with prog.context as q:
ops.S2gate(r, phi) | q
# run decomposition with reversed arguments
BS | q
ops.Sgate(-r, phi) | q[0]
ops.Sgate(r, phi) | q[1]
BS.H | q
eng.run(prog)
assert np.all(eng.backend.is_vacuum(tol))
def entangle_states(q):
ops.Sgate(-2) | q[0]
ops.Sgate(2) | q[1]
ops.BSgate(np.pi / 4, 0) | (q[0], q[1])
def test_squeeze_variance_frontend(self, setup_eng, hbar, tol):
"""test homodyne measurement of a squeeze state is correct,
returning a variance of np.exp(-2*r)*h/2, via the frontend"""
eng, prog = setup_eng(1)
with prog.context as q:
ops.Sgate(R) | q
ops.MeasureX | q
res = np.empty(0)
for i in range(N_MEAS):
eng.run(prog)
res = np.append(res, q[0].val)
eng.reset()
assert np.allclose(
np.var(res), np.exp(-2 * R) * hbar / 2, atol=STD_10 + tol, rtol=0
)
def entangle_states(q):
ops.Sgate(-2) | q[0]
ops.Sgate(2) | q[1]
ops.BSgate(np.pi / 4, 0) | (q[0], q[1])
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()
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()
# probability of meausring m1 and m2
sq_r = params[:3]
# squeezing phase
sq_phi = params[3:6]
# displacement magnitudes (assume displacement is real for now)
d_r = params[6:9]
# beamsplitter theta
bs_theta1, bs_theta2, bs_theta3 = params[9:12]
# beamsplitter phi
bs_phi1, bs_phi2, bs_phi3 = params[12:]
# quantum circuit prior to entering the beamsplitter
prog = sf.Program(3)
with prog.context as q:
for k in range(3):
Sgate(sq_r[k], sq_phi[k]) | q[k]
Dgate(d_r[k]) | q[k]
eng = sf.Engine("fock", backend_options={"cutoff_dim": cutoff})
stateIn = eng.run(prog).state
normIn = np.abs(stateIn.trace())
# norm of output state and probability
prog_BS = sf.Program(3)
with prog_BS.context as q:
BSgate(bs_theta1, bs_phi1) | (q[0], q[1])
BSgate(bs_theta2, bs_phi2) | (q[1], q[2])
BSgate(bs_theta3, bs_phi3) | (q[0], q[1])
stateOut = eng.run(prog_BS).state
normOut = np.abs(stateOut.trace())
rho = stateOut.dm()
def _decompose(self, reg, **kwargs):
# into a squeeze and a rotation
temp = self.p[0] / 2
r = pf.acosh(pf.sqrt(1+temp**2))
theta = pf.atan(temp)
phi = -np.pi / 2 * pf.sign(temp) - theta
return [
Command(Sgate(r, phi), reg),
Command(Rgate(theta), reg)
]