def test_program_subroutine(self, setup_eng, tol): """Simple quantum program with a subroutine and references.""" eng, prog = setup_eng(2) # define some gates D = ops.Dgate(0.5) BS = ops.BSgate(0.7 * np.pi, np.pi / 2) R = ops.Rgate(np.pi / 3) def subroutine(a, b): """Subroutine for the quantum program""" R | a BS | (a, b) R.H | a # main program with prog.context as q: # get register references alice, bob = q ops.All(ops.Vacuum()) | (alice, bob) D | alice subroutine(alice, bob) BS | (alice, bob) subroutine(bob, alice) state = eng.run(prog).state # state norm must be invariant if isinstance(eng.backend, BaseFock): assert np.allclose(state.trace(), 1, atol=tol, rtol=0)
def test_exceptions(self, state): """Test exceptions raised if state prep used on invalid modes""" G = state() prog = Program(2) with prog.context: # all states act on a single mode with pytest.raises(ValueError): G.__or__([0, 1]) # can't repeat the same index with pytest.raises(RegRefError): ops.All(G).__or__([0, 0])
def test_interferometer(self, setup_eng, tol): """Test applying an interferometer""" eng, q = setup_eng(3) with eng: ops.All(ops.Squeezed(0.5)) | q init = eng.run() ops.Interferometer(u1) | q state = eng.run() O = np.vstack( [np.hstack([u1.real, -u1.imag]), np.hstack([u1.imag, u1.real])]) assert np.allclose(state.cov(), O @ init.cov() @ O.T, atol=tol)
def test_interferometer(self, setup_eng, tol): """Test applying an interferometer""" eng, p1 = setup_eng(3) with p1.context as q: ops.All(ops.Squeezed(0.5)) | q init = eng.run(p1).state p2 = sf.Program(p1) with p2.context as q: ops.Interferometer(u1) | q state = eng.run(p2).state O = np.vstack([np.hstack([u1.real, -u1.imag]), np.hstack([u1.imag, u1.real])]) assert np.allclose(state.cov(), O @ init.cov() @ O.T, atol=tol)
def test_passive_gaussian_transform(self, setup_eng, tol): """Test applying a passive Gaussian symplectic transform, which is simply an interferometer""" eng, q = setup_eng(3) O = np.vstack( [np.hstack([u1.real, -u1.imag]), np.hstack([u1.imag, u1.real])]) with eng: ops.All(ops.Squeezed(0.5)) | q init = eng.run() ops.GaussianTransform(O) | q state = eng.run() assert np.allclose(state.cov(), O @ init.cov() @ O.T, atol=tol)
def test_dispatch_one_mode_gates(self, gate): """test one mode gates automatically add to the queue""" prog = sf.Program(2) G = gate(a) if G.ns == 2: pytest.skip("test only for 1 mode gates.") with prog.context: G | 0 ops.All(G) | (0, 1) assert len(prog) == 3 assert all(cmd.op == G for cmd in prog.circuit) assert prog.circuit[0].reg[0].ind == 0 assert prog.circuit[1].reg[0].ind == 0 assert prog.circuit[2].reg[0].ind == 1
def test_dispatch_one_mode_gates(self, gate): """test one mode gates automatically add to the queue""" eng, _ = sf.Engine(2) G = gate(a) if G.ns == 2: pytest.skip("test only for 1 mode gates.") with eng: G | 0 ops.All(G) | (0, 1) assert len(eng.cmd_queue) == 3 assert all(cmd.op == G for cmd in eng.cmd_queue) assert eng.cmd_queue[0].reg[0].ind == 0 assert eng.cmd_queue[1].reg[0].ind == 0 assert eng.cmd_queue[2].reg[0].ind == 1