Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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_z_0(self, tmpdir):
prog = sf.Program(3)
with prog.context as q:
ops.Zgate(1) | (q[0])
z_test_0_output = dedent(
r""" \documentclass{article}
\pagestyle{empty}
\usepackage{qcircuit}
\begin{document}
\Qcircuit {
& \gate{Z} & \qw \\
& \qw & \qw \\
& \qw & \qw \\
}
\end{document}"""
)
result = prog.draw_circuit(tex_dir=tmpdir)[1]
assert result == z_test_0_output, failure_message(result, z_test_0_output)
def test_x_z_0(self, tmpdir):
prog = sf.Program(3)
with prog.context as q:
ops.Xgate(1) | (q[0])
ops.Zgate(1) | (q[0])
x_z_test_0_output = dedent(
r""" \documentclass{article}
\pagestyle{empty}
\usepackage{qcircuit}
\begin{document}
\Qcircuit {
& \gate{X} & \gate{Z} & \qw \\
& \qw & \qw & \qw \\
& \qw & \qw & \qw \\
}
\end{document}"""
)
result = prog.draw_circuit(tex_dir=tmpdir)[1]
assert result == x_z_test_0_output, failure_message(result, x_z_test_0_output)
def test_parse_op(self, drawer):
prog = sf.Program(3)
with prog.context as q:
ops.Xgate(1) | (q[0])
ops.Zgate(1) | (q[0])
ops.CXgate(1) | (q[0], q[1])
ops.CZgate(1) | (q[0], q[1])
ops.BSgate(0, 1) | (q[0], q[1])
ops.S2gate(0, 1) | (q[0], q[1])
ops.CKgate(1) | (q[0], q[1])
ops.Kgate(1) | (q[0])
ops.Vgate(1) | (q[0])
ops.Pgate(1) | (q[0])
ops.Rgate(1) | (q[0])
ops.Sgate(1) | (q[0])
ops.Dgate(1) | (q[0])
for op in prog.circuit:
drawer.parse_op(op)
expected_circuit_matrix = [
def test_merge_incompatible(self):
"""Test merging of incompatible gates does nothing"""
prog = sf.Program(3)
with prog.context:
ops.Xgate(0.6) | 0
ops.Zgate(0.2) | 0
prog = prog.optimize()
assert len(prog) == 2
def test_CZgate(self, setup_eng, pure, hbar, tol):
"""Test the action of the CZ gate in phase space"""
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 test_regref_no_func_str(self):
"""Test a regreftransform with no function string raises exception"""
prog = Program(2)
with prog.context as q:
ops.Sgate(0.43) | q[0]
ops.MeasureX | q[0]
ops.Zgate(ops.RR(q[0], lambda x: 2 * x)) | q[1]
with pytest.raises(ValueError, match="not supported by Blackbird"):
io.to_blackbird(prog)
def test_merge_incompatible():
"""Test merging of incompatible gates does nothing"""
prog = sf.Program(3)
with prog.context:
ops.Xgate(0.6) | 0
ops.Zgate(0.2) | 0
prog.optimize()
assert len(prog) == 2
def decompose(self, reg):
"""Return the decomposed commands"""
cmds = []
cmds += [Command(GaussianTransform(self.S, hbar=self.hbar), reg, decomp=True)]
if self.disp:
cmds += [Command(Xgate(x), reg, decomp=True) for x in self.d[:self.ns] if x != 0.]
cmds += [Command(Zgate(z), reg, decomp=True) for z in self.d[self.ns:] if z != 0.]
return cmds
# 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