Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.ns = S.shape[0] // 2
self.vacuum = vacuum #: bool: if True, ignore the first unitary matrix when applying the gate
N = self.ns # shorthand
# check if input symplectic is passive (orthogonal)
diffn = np.linalg.norm(S @ S.T - np.identity(2*N))
self.active = (np.abs(diffn) > _decomposition_tol) #: bool: S is an active symplectic transformation
if not self.active:
# The transformation is passive, do Clements
X1 = S[:N, :N]
P1 = S[N:, :N]
self.U1 = X1+1j*P1
else:
# transformation is active, do Bloch-Messiah
O1, smat, O2 = dec.bloch_messiah(S, tol=tol)
X1 = O1[:N, :N]
P1 = O1[N:, :N]
X2 = O2[:N, :N]
P2 = O2[N:, :N]
self.U1 = X1+1j*P1 #: array[complex]: unitary matrix corresponding to O_1
self.U2 = X2+1j*P2 #: array[complex]: unitary matrix corresponding to O_2
self.Sq = np.diagonal(smat)[:N] #: array[complex]: diagonal vector of the squeezing matrix R
def test_make_traceless_deprecated(self, monkeypatch, tol):
"""Test that A is properly made traceless"""
A = np.random.random([6, 6]) + 1j * np.random.random([6, 6])
A += A.T
assert not np.allclose(np.trace(A), 0, atol=tol, rtol=0)
with monkeypatch.context() as m:
# monkeypatch the takagi function to simply return A,
# so that we can inspect it and make sure it is now traceless
m.setattr(dec, "takagi", lambda A, tol: (np.ones([6]), A))
_, A_out = dec.graph_embed_deprecated(A, make_traceless=True)
assert np.allclose(np.trace(A_out), 0, atol=tol, rtol=0)
def test_active_transform(self, create_transform, tol):
"""Test passive transform with squeezing"""
n = 3
S_in = create_transform(3, passive=False)
O1, S, O2 = dec.bloch_messiah(S_in)
# test decomposition
assert np.allclose(O1 @ S @ O2, S_in, atol=tol, rtol=0)
# test orthogonality
assert np.allclose(O1.T @ O1, np.identity(2 * n), atol=tol, rtol=0)
assert np.allclose(O2.T @ O2, np.identity(2 * n), atol=tol, rtol=0)
# test symplectic
O = omega(n)
assert np.allclose(O1.T @ O @ O1, O, atol=tol, rtol=0)
assert np.allclose(O2.T @ O @ O2, O, atol=tol, rtol=0)
assert np.allclose(S @ O @ S.T, O, atol=tol, rtol=0)
various unitary matrices.
A given unitary (identity or random draw from Haar measure) is
decomposed using the function :func:`dec.rectangular_symmetric`
and the resulting beamsplitters are multiplied together.
Test passes if the product matches identity.
"""
nmax, mmax = U.shape
assert nmax == mmax
tlist, diags, _ = dec.rectangular_symmetric(U)
qrec = np.identity(nmax)
for i in tlist:
assert i[2] >= 0 and i[2] < 2 * np.pi # internal phase
assert i[3] >= 0 and i[3] < 2 * np.pi # external phase
qrec = dec.mach_zehnder(*i) @ qrec
qrec = np.diag(diags) @ qrec
assert np.allclose(U, qrec, atol=tol, rtol=0)
"""Test that an graph is correctly decomposed when the interferometer
has one zero somewhere in the unitary matrix, which is the case for the
adjacency matrix below"""
n = 6
prog = sf.Program(n)
A = np.array([
[0, 1, 0, 0, 1, 1],
[1, 0, 1, 0, 1, 1],
[0, 1, 0, 1, 1, 0],
[0, 0, 1, 0, 1, 0],
[1, 1, 1, 1, 0, 1],
[1, 1, 0, 0, 1, 0],
]
)
sq, U = dec.graph_embed(A)
assert not np.allclose(U, np.identity(n))
G = ops.GraphEmbed(A)
cmds = G.decompose(prog.register)
last_op = cmds[-1].op
param_val = last_op.p[0].x
assert isinstance(last_op, ops.Interferometer)
assert last_op.ns == n
assert np.allclose(param_val, U, atol=tol, rtol=0)
def test_decomposition(self, hbar, tol):
"""Test that a graph is correctly decomposed"""
n = 3
prog = sf.Program(2*n)
A = np.zeros([2*n, 2*n])
B = np.random.random([n, n])
A[:n, n:] = B
A += A.T
sq, U, V = dec.bipartite_graph_embed(B)
G = ops.BipartiteGraphEmbed(A)
cmds = G.decompose(prog.register)
S = np.identity(4 * n)
# calculating the resulting decomposed symplectic
for cmd in cmds:
# all operations should be BSgates, Rgates, or S2gates
assert isinstance(
cmd.op, (ops.Interferometer, ops.S2gate)
)
# build up the symplectic transform
modes = [i.ind for i in cmd.reg]
def test_random_unitary_phase_end(self, tol):
"""This test checks the rectangular decomposition with phases at the end.
A random unitary is drawn from the Haar measure, then is decomposed
using Eq. 5 of the rectangular decomposition procedure of Clements et al,
i.e., moving all the phases to the end of the interferometer. The
resulting beamsplitters are multiplied together. Test passes if the
product matches the drawn unitary.
"""
n = 20
U = haar_measure(n)
tlist, diags, _ = dec.rectangular_phase_end(U)
qrec = np.identity(n)
for i in tlist:
qrec = dec.T(*i) @ qrec
qrec = np.diag(diags) @ qrec
assert np.allclose(U, qrec, atol=tol, rtol=0)
def test_make_traceless_deprecated(self, monkeypatch, tol):
"""Test that A is properly made traceless"""
A = np.random.random([6, 6]) + 1j * np.random.random([6, 6])
A += A.T
assert not np.allclose(np.trace(A), 0, atol=tol, rtol=0)
with monkeypatch.context() as m:
# monkeypatch the takagi function to simply return A,
# so that we can inspect it and make sure it is now traceless
m.setattr(dec, "takagi", lambda A, tol: (np.ones([6]), A))
_, A_out = dec.graph_embed_deprecated(A, make_traceless=True)
assert np.allclose(np.trace(A_out), 0, atol=tol, rtol=0)
def test_random_unitary(self, tol):
"""This test checks the rectangular decomposition for a random unitary.
A random unitary is drawn from the Haar measure, then is decomposed via
the rectangular decomposition of Clements et al., and the resulting
beamsplitters are multiplied together. Test passes if the product
matches the drawn unitary.
"""
# TODO: this test currently uses the T and Ti functions used to compute
# Clements as the comparison. Probably should be changed.
n = 20
U = haar_measure(n)
tilist, diags, tlist = dec.rectangular(U)
qrec = np.identity(n)
for i in tilist:
qrec = dec.T(*i) @ qrec
qrec = np.diag(diags) @ qrec
for i in reversed(tlist):
qrec = dec.Ti(*i) @ qrec
assert np.allclose(U, qrec, atol=tol, rtol=0)
def _decompose(self, reg, **kwargs):
mean_photon_per_mode = kwargs.get("mean_photon_per_mode", self.mean_photon_per_mode)
tol = kwargs.get("tol", self.tol)
mesh = kwargs.get("mesh", "rectangular")
drop_identity = kwargs.get("drop_identity", self.drop_identity)
cmds = []
B = self.p[0]
N = len(B)
sq, U, V = dec.bipartite_graph_embed(B, mean_photon_per_mode=mean_photon_per_mode, atol=tol, rtol=0)
if not self.identity or not drop_identity:
for m, s in enumerate(sq):
s = s if np.abs(s) >= _decomposition_tol else 0
if not (drop_identity and s == 0):
cmds.append(Command(S2gate(-s), (reg[m], reg[m+N])))
for X, _reg in ((U, reg[:N]), (V, reg[N:])):
if np.allclose(X, np.identity(len(X)), atol=_decomposition_tol, rtol=0):
X = np.identity(len(X))
if not (drop_identity and np.all(X == np.identity(len(X)))):
cmds.append(Command(Interferometer(X, mesh=mesh, drop_identity=drop_identity, tol=tol), _reg))