def test_dm_two_mode(self, setup_eng, hbar, cutoff, tol): """Tests multimode dm preparation""" eng, prog = setup_eng(2) ket0 = np.random.uniform( -1, 1, cutoff) + 1j * np.random.uniform(-1, 1, cutoff) ket0 = ket0 / np.linalg.norm(ket0) ket1 = np.random.uniform( -1, 1, cutoff) + 1j * np.random.uniform(-1, 1, cutoff) ket1 = ket1 / np.linalg.norm(ket1) ket = np.outer(ket0, ket1) rho = np.einsum("ij,kl->ikjl", ket, ket.conj()) with prog.context as q: ops.DensityMatrix(rho) | q state = eng.run(prog).state assert np.allclose(state.dm(), rho, atol=tol, rtol=0) eng.reset() prog = sf.Program(2) state1 = BaseFockState(rho, 2, False, cutoff) with prog.context as q: ops.DensityMatrix(state1) | q state2 = eng.run(prog).state assert np.allclose(state1.dm(), state2.dm(), atol=tol, rtol=0)
def setup_two_mode_circuit(self, setup_eng, cutoff): """Create the circuit for following tests""" eng_ref, q = setup_eng(2) S = ops.Sgate(2) B = ops.BSgate(2.234, -1.165) initial_state = np.complex64( np.random.rand(*[cutoff] * 4) + 1j * np.random.rand(*[cutoff] * 4)) with eng_ref: ops.DensityMatrix(initial_state) | q S | q[0] B | q S | q[1] B | q eng, q = sf.Engine(2) with eng: S | q[0] B | q S | q[1] B | q rho = eng_ref.run().dm() return eng, rho, initial_state
def test_dm_one_mode(self, setup_eng, hbar, cutoff, tol): """Tests single mode DM preparation""" eng, prog = setup_eng(2) ket = np.random.uniform(-1, 1, cutoff) + 1j * np.random.uniform(-1, 1, cutoff) ket = ket / np.linalg.norm(ket) rho = np.outer(ket, ket.conj()) with prog.context as q: ops.DensityMatrix(rho) | q[0] state = eng.run(prog, modes=[0]) assert np.allclose(state.dm(), rho, atol=tol, rtol=0) eng.reset() prog = sf.Program(2) state1 = BaseFockState(rho, 1, False, cutoff, hbar) with prog.context as q: ops.DensityMatrix(state1) | q[0] state2 = eng.run(prog, modes=[0]) assert np.allclose(state1.dm(), state2.dm(), atol=tol, rtol=0)
def test_dm_input_validation(self, setup_eng, hbar, cutoff, tol): """Test exceptions""" mu = np.array([0.0, 0.0]) cov = np.identity(2) state = GaussianState((mu, cov), 1, True, hbar) eng, prog = setup_eng(2) with prog.context as q: with pytest.raises(ValueError, match="Gaussian states are not supported"): ops.DensityMatrix(state) | q[0]
def test_dm_gaussian_state_object(self, setup_eng): """Test exception if loading a ket from a Gaussian state object""" eng = sf.Engine('gaussian') prog = sf.Program(1) state = eng.run(prog) # create new engine eng, prog = setup_eng(1) with prog.context as q: with pytest.raises(ValueError, match="Gaussian states are not supported"): ops.DensityMatrix(state) | q[0]
def test_dm_state_object(self, setup_eng, tol): """Test loading a density matrix from a prior state object""" eng, prog = setup_eng(1) with prog.context as q: ops.Dgate(0.2) | q[0] state1 = eng.run(prog).state # create new engine eng, prog = setup_eng(1) with prog.context as q: ops.DensityMatrix(state1) | q[0] state2 = eng.run(prog).state # verify it is the same state assert np.allclose(state1.dm(), state2.dm(), atol=tol, rtol=0)
def setup_one_mode_circuit(self, setup_eng, cutoff): """Create the circuit for following tests""" eng_ref, p0 = setup_eng(1) S = ops.Sgate(1.1, -1.4) L = ops.LossChannel(0.45) initial_state = np.random.rand(cutoff, cutoff) with p0.context as q: ops.DensityMatrix(initial_state) | q prog = sf.Program(p0) with prog.context as q: S | q L | q rho = eng_ref.run([p0, prog]).state.dm() return prog, rho, initial_state
def setup_two_mode_circuit(self, setup_eng, cutoff): """Create the circuit for following tests""" eng_ref, p0 = setup_eng(2) S = ops.Sgate(2) B = ops.BSgate(2.234, -1.165) initial_state = np.complex64( np.random.rand(*[cutoff] * 4) + 1j * np.random.rand(*[cutoff] * 4)) with p0.context as q: ops.DensityMatrix(initial_state) | q prog = sf.Program(p0) with prog.context as q: S | q[0] B | q S | q[1] B | q rho = eng_ref.run([p0, prog]).state.dm() return prog, rho, initial_state
def setup_one_mode_circuit(self, setup_eng, cutoff): """Create the circuit for following tests""" eng_ref, q = setup_eng(1) S = ops.Sgate(1.1, -1.4) L = ops.LossChannel(0.45) initial_state = np.random.rand(cutoff, cutoff) with eng_ref: ops.DensityMatrix(initial_state) | q S | q L | q eng, q = sf.Engine(1) with eng: S | q L | q rho = eng_ref.run().dm() return eng, rho, initial_state