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_compile_custom_dir(self, tmpdir):
prog = sf.Program(3)
with prog.context as q:
ops.Dgate(1) | (q[1])
ops.Rgate(1) | (q[1])
ops.S2gate(1) | (q[0], q[1])
subdir = tmpdir.join("subdir")
document = prog.draw_circuit(tex_dir=subdir)[0]
file_name = "output_{0}.tex".format(
datetime.datetime.now().strftime("%Y_%B_%d_%I:%M%p")
)
assert document.split("/")[-1] == file_name
output_file = subdir.join(file_name)
assert os.path.isfile(output_file)
with expected.context as q:
ops.S2gate(SQ_AMPLITUDE, 0) | (q[0], q[4])
ops.S2gate(SQ_AMPLITUDE, 0) | (q[1], q[5])
ops.S2gate(SQ_AMPLITUDE, 0) | (q[2], q[6])
ops.S2gate(SQ_AMPLITUDE, 0) | (q[3], q[7])
# corresponds to an identity on modes [0, 1, 2, 3]
ops.MZgate(0, 0) | (q[0], q[1])
ops.MZgate(0, 0) | (q[2], q[3])
ops.MZgate(np.pi, np.pi) | (q[1], q[2])
ops.MZgate(0, 0) | (q[0], q[1])
ops.MZgate(0, 0) | (q[2], q[3])
ops.MZgate(0, np.pi) | (q[1], q[2])
ops.Rgate(np.pi) | (q[0])
ops.Rgate(0) | (q[1])
ops.Rgate(np.pi) | (q[2])
ops.Rgate(-np.pi) | (q[3])
# corresponds to an identity on modes [4, 5, 6, 7]
ops.MZgate(0, 0) | (q[4], q[5])
ops.MZgate(0, 0) | (q[6], q[7])
ops.MZgate(np.pi, np.pi) | (q[5], q[6])
ops.MZgate(0, 0) | (q[4], q[5])
ops.MZgate(0, 0) | (q[6], q[7])
ops.MZgate(0, np.pi) | (q[5], q[6])
ops.Rgate(np.pi) | (q[4])
ops.Rgate(0) | (q[5])
ops.Rgate(np.pi) | (q[6])
ops.Rgate(-np.pi) | (q[7])
ops.MeasureFock() | q
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 check_reg(p, expected_n=None):
"""Compare Program.register with the mode list returned by the backend.
ops.S2gate(0.5) | (q[0], q[2])
ops.S2gate(0.5) | (q[1], q[3])
ops.BSgate() | (q[0], q[1])
ops.BSgate() | (q[2], q[3])
ops.MeasureFock() | q
res = prog.compile("chip0")
expected = sf.Program(4)
with expected.context as q:
ops.S2gate(0.5, 0) | (q[0], q[2])
ops.S2gate(0.5, 0) | (q[1], q[3])
# corresponds to BSgate() on modes [0, 1]
ops.Rgate(0) | (q[0])
ops.BSgate(np.pi / 4, np.pi / 2) | (q[0], q[1])
ops.Rgate(3 * np.pi / 2) | (q[0])
ops.BSgate(np.pi / 4, np.pi / 2) | (q[0], q[1])
ops.Rgate(3 * np.pi / 4) | (q[0])
ops.Rgate(-np.pi / 4) | (q[1])
# corresponds to BSgate() on modes [2, 3]
ops.Rgate(0) | (q[2])
ops.BSgate(np.pi / 4, np.pi / 2) | (q[2], q[3])
ops.Rgate(3 * np.pi / 2) | (q[2])
ops.BSgate(np.pi / 4, np.pi / 2) | (q[2], q[3])
ops.Rgate(3 * np.pi / 4) | (q[2])
ops.Rgate(-np.pi / 4) | (q[3])
ops.MeasureFock() | q
def test_one_mode_gates_from_operators(self, drawer):
prog = sf.Program(3)
with prog.context as q:
ops.Xgate(1) | (q[0])
ops.Zgate(1) | (q[0])
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:
method, mode = drawer._gate_from_operator(op)
assert callable(method) and hasattr(drawer, method.__name__)
assert mode == 1
def test_Pgate_decomp_equal(self, setup_eng, s, tol):
"""Tests that the Pgate gives the same transformation as its decomposition."""
eng, prog = setup_eng(1)
r = np.arccosh(np.sqrt(1 + (s / 2) ** 2))
theta = np.arctan(s / 2)
phi = -np.sign(s) * np.pi / 2 - theta
with prog.context as q:
ops.Pgate(s) | q
# run decomposition with reversed arguments
ops.Rgate(-theta) | q
ops.Sgate(r, phi + np.pi) | q
eng.run(prog)
assert np.all(eng.backend.is_vacuum(tol))
"""
name = 'Strawberry Fields Gaussian PennyLane plugin'
short_name = 'strawberryfields.gaussian'
_operation_map = {
'CoherentState': Coherent,
'DisplacedSqueezedState': DisplacedSqueezed,
'SqueezedState': Squeezed,
'ThermalState': Thermal,
'GaussianState': Gaussian,
'Beamsplitter': BSgate,
'ControlledAddition': CXgate,
'ControlledPhase': CZgate,
'Displacement': Dgate,
'QuadraticPhase': Pgate,
'Rotation': Rgate,
'TwoModeSqueezing': S2gate,
'Squeezing': Sgate,
'Interferometer': Interferometer
}
_observable_map = {
'NumberOperator': mean_photon,
'TensorN': number_expectation,
'X': homodyne(0),
'P': homodyne(np.pi/2),
'QuadOperator': homodyne(),
'PolyXP': poly_xp,
'FockStateProjector': fock_state,
'Identity': identity
}
def _decompose(self, reg, **kwargs):
# into local phase shifts and two 50-50 beamsplitters
return [
Command(Rgate(self.p[0]), reg[0]),
Command(BSgate(np.pi/4, np.pi/2), reg),
Command(Rgate(self.p[1]), reg[0]),
Command(BSgate(np.pi/4, np.pi/2), reg)
]
'CoherentState': Coherent,
'FockDensityMatrix': DensityMatrix,
'DisplacedSqueezedState': DisplacedSqueezed,
'FockState': Fock,
'FockStateVector': Ket,
'SqueezedState': Squeezed,
'ThermalState': Thermal,
'GaussianState': Gaussian,
'Beamsplitter': BSgate,
'CrossKerr': CKgate,
'ControlledAddition': CXgate,
'ControlledPhase': CZgate,
'Displacement': Dgate,
'Kerr': Kgate,
'QuadraticPhase': Pgate,
'Rotation': Rgate,
'TwoModeSqueezing': S2gate,
'Squeezing': Sgate,
'CubicPhase': Vgate
}
_observable_map = {
'PhotonNumber': PNR,
'X': Homodyne(0),
'P': Homodyne(np.pi/2),
'Homodyne': Homodyne(),
'PolyXP': Order2Poly,
}
_circuits = {}
def __init__(self, wires, *, shots=0, cutoff_dim, hbar=2):