Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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])
ops.Fourier | q[0]
ops.Xgate(0.4) | q[1]
ops.Zgate(0.5) | q[3]
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_x_1(self, tmpdir):
prog = sf.Program(3)
with prog.context as q:
ops.Xgate(1) | (q[1])
x_test_1_output = dedent(
r""" \documentclass{article}
\pagestyle{empty}
\usepackage{qcircuit}
\begin{document}
\Qcircuit {
& \qw & \qw \\
& \gate{X} & \qw \\
& \qw & \qw \\
}
\end{document}"""
)
result = prog.draw_circuit(tex_dir=tmpdir)[1]
assert result == x_test_1_output, failure_message(result, x_test_1_output)
def test_x_0(self, tmpdir):
prog = sf.Program(3)
with prog.context as q:
ops.Xgate(1) | (q[0])
x_test_0_output = dedent(
r""" \documentclass{article}
\pagestyle{empty}
\usepackage{qcircuit}
\begin{document}
\Qcircuit {
& \gate{X} & \qw \\
& \qw & \qw \\
& \qw & \qw \\
}
\end{document}"""
)
result = prog.draw_circuit(tex_dir=tmpdir)[1]
assert result == x_test_0_output, failure_message(result, x_test_0_output)
def test_xx_1(self, tmpdir):
prog = sf.Program(3)
with prog.context as q:
ops.Xgate(1) | (q[1])
ops.Xgate(1) | (q[1])
xx_test_1_output = dedent(
r""" \documentclass{article}
\pagestyle{empty}
\usepackage{qcircuit}
\begin{document}
\Qcircuit {
& \qw & \qw & \qw \\
& \gate{X} & \gate{X} & \qw \\
& \qw & \qw & \qw \\
}
\end{document}"""
)
result = prog.draw_circuit(tex_dir=tmpdir)[1]
if not pure:
pytest.skip("Test only runs on pure states")
N = 2
eng, prog = setup_eng(N)
r = 3
x1 = 2
x2 = 1
p1 = 1.37
p2 = 2.71
s = 0.5
with prog.context as q:
ops.Sgate(r) | q[0]
ops.Xgate(x1) | q[0]
ops.Zgate(p1) | q[0]
ops.Sgate(r) | q[1]
ops.Xgate(x2) | q[1]
ops.Zgate(p2) | q[1]
ops.CZgate(s) | q
state = eng.run(prog).state
CZmat = np.array([[1, 0, 0, 0], [0, 1, 0, 0], [0, s, 1, 0], [s, 0, 0, 1]])
Vexpected = 0.5 * hbar * CZmat @ np.diag(np.exp([-2 * r, -2 * r, 2 * r, 2 * r])) @ CZmat.T
# Checks the covariance matrix is transformed correctly
assert np.allclose(state.cov(), Vexpected, atol=tol, rtol=0)
rexpected = CZmat @ np.array([x1, x2, p1, p2])
# Checks the means are transformed correctly
assert np.allclose(state.means(), rexpected, atol=tol, rtol=0)
def Q(self):
"""The common test gate"""
return ops.Xgate(0.5)
def test_x_displacement(self, setup_eng, hbar, tol):
"""test x displacement on the Gaussian backend gives correct displacement"""
eng, prog = setup_eng(1)
with prog.context as q:
ops.Xgate(X) | q
state = eng.run(prog).state
mu_x = state.means()[0]
assert state.hbar == hbar
assert np.allclose(mu_x, X, atol=tol, rtol=0)
else:
if not self.pure:
# mixed state, must initialise thermal states
for n, nbar in enumerate(self.nbar):
if np.abs(nbar) >= _decomposition_tol:
cmds.append(Command(Thermal(nbar), reg[n]))
else:
cmds.append(Command(Vac, reg[n]))
else:
for r in reg:
cmds.append(Command(Vac, r))
cmds.append(Command(GaussianTransform(self.S, vacuum=self.pure), reg))
cmds += [Command(Xgate(u), reg[n])
for n, u in enumerate(self.x_disp) if u != 0]
cmds += [Command(Zgate(u), reg[n])
for n, u in enumerate(self.p_disp) if u != 0]
return cmds
Del = _Delete()
Vac = Vacuum()
MeasureX = MeasureHomodyne(0)
MeasureP = MeasureHomodyne(np.pi / 2)
MeasureHD = MeasureHeterodyne()
Fourier = Fouriergate()
shorthands = ['New', 'Del', 'Vac', 'MeasureX', 'MeasureP', 'MeasureHD', 'Fourier', 'All']
#=======================================================================
# here we list different classes of operations for unit testing purposes
zero_args_gates = (Fouriergate,)
one_args_gates = (Xgate, Zgate, Rgate, Pgate, Vgate,
Kgate, CXgate, CZgate, CKgate)
two_args_gates = (Dgate, Sgate, BSgate, MZgate, S2gate)
gates = zero_args_gates + one_args_gates + two_args_gates
channels = (LossChannel, ThermalLossChannel)
simple_state_preparations = (Vacuum, Coherent, Squeezed, DisplacedSqueezed, Fock, Catstate, Thermal) # have __init__ methods with default arguments
state_preparations = simple_state_preparations + (Ket, DensityMatrix)
measurements = (MeasureFock, MeasureHomodyne, MeasureHeterodyne, MeasureThreshold)
decompositions = (Interferometer, BipartiteGraphEmbed, GraphEmbed, GaussianTransform, Gaussian)
#=======================================================================
# exported symbols