def test_merge(self, hbar, tol): """Test that two covariances matrices overwrite each other on merge""" n = 3 V1 = random_covariance(n, pure=False, hbar=hbar) V2 = random_covariance(n, pure=True, hbar=hbar) cov1 = ops.Gaussian(V1, hbar=hbar) cov2 = ops.Gaussian(V2, hbar=hbar) # applying a second covariance matrix replaces the first assert cov1.merge(cov2) == cov2 # the same is true of state preparations assert ops.Squeezed(2).merge(cov2) == cov2
def test_decomposition(self, hbar, tol): """Test that an arbitrary decomposition provides the right covariance matrix""" n = 3 prog = sf.Program(n) cov = random_covariance(n) G = ops.Gaussian(cov) cmds = G.decompose(prog.register) S = np.identity(2 * n) cov_init = np.identity(2 * n) * hbar / 2 # calculating the resulting decomposed symplectic for cmd in cmds: # all operations should be BSgates, Rgates, or Sgates assert isinstance(cmd.op, (ops.Vacuum, ops.Thermal, ops.GaussianTransform)) # build up the symplectic transform modes = [i.ind for i in cmd.reg] if isinstance(cmd.op, ops.Thermal): cov_init[cmd.reg[0].ind, cmd.reg[0].ind] = ((2 * cmd.op.p[0].x + 1) * hbar / 2) cov_init[cmd.reg[0].ind + n, cmd.reg[0].ind + n] = ((2 * cmd.op.p[0].x + 1) * hbar / 2) if isinstance(cmd.op, ops.GaussianTransform): S = cmd.op.p[0].x @ S # the resulting covariance state cov_res = S @ cov_init @ S.T assert np.allclose(cov, cov_res, atol=tol, rtol=0)
def test_user_defined_decomposition_false(self): """Test that an operation that is both a primitive AND a decomposition (for instance, ops.Gaussian in the gaussian backend) can have it's decomposition behaviour user defined. In this case, the Gaussian operation should remain after compilation. """ prog = sf.Program(2) cov = np.ones((4, 4)) + np.eye(4) r = np.array([0, 1, 1, 2]) with prog.context as q: ops.Gaussian(cov, r, decomp=False) | q prog = prog.compile(target='gaussian') assert len(prog) == 1 circuit = prog.circuit assert circuit[0].op.__class__.__name__ == "Gaussian" # test compilation against multiple targets in sequence with pytest.raises( program.CircuitError, match= "The operation Gaussian is not a primitive for the target 'fock'" ): prog = prog.compile(target='fock')
def test_rotated_squeezed_decomposition(self, hbar, tol): """Test that a rotated squeeze state decomposition provides the correct covariance matrix""" n = 3 prog = sf.Program(n, hbar=hbar) sq_r = np.array([0.453, 0.23, 0.543]) sq_phi = np.array([-0.123, 0.2143, 0.021]) S = np.diag(np.exp(np.concatenate([-sq_r, sq_r]))) for i, phi in enumerate(sq_phi): S = _rotation(phi / 2, i, n) @ S cov = S @ S.T with eng: G = ops.Gaussian(cov) cmds = G.decompose(q) assert len(cmds) == n # calculating the resulting decomposed symplectic for i, cmd in enumerate(cmds): # all operations should be Sgates assert isinstance(cmd.op, ops.Sgate) assert np.allclose(cmd.op.p[0].x, sq_r[i], atol=tol, rtol=0) assert np.allclose(cmd.op.p[1].x, sq_phi[i], atol=tol, rtol=0)
def test_setting_hbar(self, hbar): """Test that an exception is raised if hbar not provided""" prog = sf.Program(3, hbar=hbar) cov = random_covariance(3, hbar=hbar) with pytest.raises(ValueError, match="specify the hbar keyword argument"): ops.Gaussian(cov) # hbar can be passed as a keyword arg G = ops.Gaussian(cov, hbar=hbar) assert G.hbar == hbar # or determined via the engine context with eng: G = ops.Gaussian(cov) assert G.hbar == hbar
def test_covariance_random_state_pure(self, setup_eng, V_pure, tol): """Test applying a pure covariance state""" eng, prog = setup_eng(3) with prog.context as q: ops.Gaussian(V_pure) | q state = eng.run(prog).state assert np.allclose(state.cov(), V_pure, atol=tol)
def test_covariance_random_state_mixed(self, setup_eng, V_mixed, tol): """Test applying a mixed covariance state""" eng, prog = setup_eng(3) with prog.context as q: ops.Gaussian(V_mixed) | q state = eng.run(prog) assert np.allclose(state.cov(), V_mixed, atol=tol)
def test_merge(self, hbar, tol): """Test that merging two Preparations only keeps the latter one.""" n = 3 V1 = random_covariance(n, pure=False, hbar=hbar) V2 = random_covariance(n, pure=True, hbar=hbar) r1 = np.random.randn(2 * n) r2 = np.random.randn(2 * n) G1 = ops.Gaussian(V1, r1) G2 = ops.Gaussian(V2, r2) # applying a second state preparation replaces the first assert G1.merge(G2) is G2 # the same is true of all state preparations S = ops.Squeezed(2) assert S.merge(G2) is G2 assert G2.merge(S) is S
def test_covariance_random_state_pure(self, setup_eng, V_pure, tol): """Test applying a pure covariance state""" eng, q = setup_eng(3) with eng: ops.Gaussian(V_pure) | q state = eng.run() assert np.allclose(state.cov(), V_pure, atol=tol)
def test_vacuum(self, setup_eng, hbar, tol): eng, prog = setup_eng(3) with prog.context as q: ops.Gaussian(np.identity(6) * hbar / 2) | q state = eng.run(prog).state assert np.allclose(state.fidelity_vacuum(), 1, atol=tol) assert len(eng.run_progs[-1]) == 3
def test_vacuum(self, setup_eng, hbar, tol): eng, q = setup_eng(3) with eng: ops.Gaussian(np.identity(6) * hbar / 2) | q state = eng.run() assert len(eng.cmd_applied[0]) == 0 assert np.allclose(state.fidelity_vacuum(), 1, atol=tol)
def test_squeezed(self, setup_eng, hbar, tol): """Testing a squeezed state""" eng, prog = setup_eng(3) cov = (hbar / 2) * np.diag([np.exp(-0.1)] * 3 + [np.exp(0.1)] * 3) with prog.context as q: ops.Gaussian(cov, decomp=False) | q state = eng.run(prog).state assert np.allclose(state.cov(), cov, atol=tol)
def test_displaced_squeezed(self, setup_eng, hbar, tol): """Testing a displaced squeezed state""" eng, q = setup_eng(3) cov = (hbar / 2) * np.diag([np.exp(-0.1)] * 3 + [np.exp(0.1)] * 3) with eng: ops.Gaussian(cov, r=[0, 0.1, 0.2, -0.1, 0.3, 0], decomp=False) | q state = eng.run() assert np.allclose(state.cov(), cov, atol=tol)
def test_thermal(self, setup_eng, hbar, tol): """Testing a thermal state""" eng, prog = setup_eng(3) cov = np.diag(hbar * (np.array([0.3, 0.4, 0.2] * 2) + 0.5)) with prog.context as q: ops.Gaussian(cov, decomp=False) | q state = eng.run(prog).state assert np.allclose(state.cov(), cov, atol=tol)
def test_apply_decomp(self, hbar): """Test that the apply method, when decomp = True, raises a NotImplemented error.""" prog = sf.Program(3, hbar=hbar) cov = random_covariance(3, hbar=hbar) with eng: G = ops.Gaussian(cov, decomp=True) with pytest.raises(NotImplementedError): G._apply(q, None)
def test_random_state_pure(self, setup_eng, V_pure, r_means, tol): """Test applying a pure covariance state""" eng, prog = setup_eng(3) with prog.context as q: ops.Gaussian(V_pure, r_means) | q state = eng.run(prog).state assert np.allclose(state.cov(), V_pure, atol=tol, rtol=0) assert np.allclose(state.means(), r_means, atol=tol, rtol=0) assert len(eng.run_progs[-1]) == 21
def test_thermal(self, setup_eng, hbar, tol): """Testing decomposed thermal state""" eng, q = setup_eng(3) cov = np.diag(hbar * (np.array([0.3, 0.4, 0.2] * 2) + 0.5)) with eng: ops.Gaussian(cov) | q state = eng.run() assert np.allclose(state.cov(), cov, atol=tol) assert np.all(len(eng.cmd_applied[0]) == 3)
def test_squeezed(self, setup_eng, hbar, tol): """Testing decomposed squeezed state""" eng, q = setup_eng(3) cov = (hbar / 2) * np.diag([np.exp(-0.1)] * 3 + [np.exp(0.1)] * 3) with eng: ops.Gaussian(cov) | q state = eng.run() assert np.allclose(state.cov(), cov, atol=tol) assert np.all(len(eng.cmd_applied[0]) == 3)
def test_displaced_squeezed(self, setup_eng, hbar, tol): """Testing decomposed displaced squeezed state""" eng, prog = setup_eng(3) cov = (hbar / 2) * np.diag([np.exp(-0.1)] * 3 + [np.exp(0.1)] * 3) with prog.context as q: ops.Gaussian(cov, r=[0, 0.1, 0.2, -0.1, 0.3, 0]) | q state = eng.run(prog) assert np.allclose(state.cov(), cov, atol=tol) assert len(eng.run_progs[-1]) == 7
def test_vacuum(self, setup_eng, hbar, tol): """Testing a vacuum state""" eng, prog = setup_eng(3) cov = (hbar / 2) * np.identity(6) with prog.context as q: ops.Gaussian(cov, decomp=False) | q state = eng.run(prog).state assert np.allclose(state.cov(), cov, atol=tol) assert np.all(state.means() == np.zeros([6])) assert np.allclose(state.fidelity_vacuum(), 1, atol=tol)
def test_displaced_squeezed(self, setup_eng, hbar, tol): """Testing a displaced squeezed state""" eng, prog = setup_eng(3) cov = (hbar / 2) * np.diag([np.exp(-0.1)] * 3 + [np.exp(0.1)] * 3) means = np.array([0, 0.1, 0.2, -0.1, 0.3, 0]) with prog.context as q: ops.Gaussian(cov, r=means, decomp=False) | q state = eng.run(prog).state assert np.allclose(state.cov(), cov, atol=tol) assert np.allclose(state.means(), means, atol=tol)
def test_vacuum(self, setup_eng, hbar, tol): """Testing a vacuum state""" eng, prog = setup_eng(3) with prog.context as q: ops.Gaussian(np.identity(6) * hbar / 2, decomp=False) | q state = eng.run(prog) cov = state.cov() means = state.means() assert np.all(cov == np.identity(6)) assert np.all(means == np.zeros([6]))
def test_vacuum(self, setup_eng, hbar, tol): """Testing decomposed vacuum state""" eng, q = setup_eng(3) with eng: ops.Gaussian(np.identity(6) * hbar / 2) | q state = eng.run() cov = state.cov() means = state.means() assert np.all(cov == np.identity(6)) assert np.all(means == np.zeros([6])) assert len(eng.cmd_applied[0]) == 0
def test_squeezed(self, setup_eng, hbar, tol): """Testing a squeezed state""" eng, prog = setup_eng(3) cov = (hbar / 2) * np.diag([np.exp(-0.1)] * 3 + [np.exp(0.1)] * 3) with prog.context as q: ops.Gaussian(cov, decomp=False) | q state = eng.run(prog).state indices = from_xp(3) cov = cov[:,indices][indices,:] assert np.allclose(state.covs(), np.expand_dims(cov,axis=0), atol=tol)
def test_thermal(self, setup_eng, hbar, tol): """Testing a thermal state""" eng, prog = setup_eng(3) cov = np.diag(hbar * (np.array([0.3, 0.4, 0.2] * 2) + 0.5)) with prog.context as q: ops.Gaussian(cov, decomp=False) | q state = eng.run(prog).state indices = from_xp(3) cov = cov[:,indices][indices,:] assert np.allclose(state.covs(), np.expand_dims(cov,axis=0), atol=tol)
def test_apply_decomp(self, hbar): """Test that the apply method, when decomp = False, calls the Backend directly.""" prog = sf.Program(3) cov = random_covariance(3, hbar=hbar) class DummyBackend: """Dummy backend class""" def prepare_gaussian_state(*args): """Raises a syntax error when called""" raise SyntaxError G = ops.Gaussian(cov, decomp=False) with pytest.raises(SyntaxError): G._apply(prog.register, DummyBackend())
def test_rotated_squeezed(self, setup_eng, hbar, tol): """Testing a rotated squeezed state""" eng, prog = setup_eng(3) r = 0.1 phi = 0.2312 v1 = (hbar / 2) * np.diag([np.exp(-r), np.exp(r)]) cov = xpxp_to_xxpp(block_diag(*[rot(phi) @ v1 @ rot(phi).T] * 3)) with prog.context as q: ops.Gaussian(cov, decomp=False) | q state = eng.run(prog).state assert np.allclose(state.cov(), cov, atol=tol)
def test_squeezed(self, setup_eng, cutoff, hbar, tol): eng, q = setup_eng(3) r = 0.05 phi = 0 cov = (hbar / 2) * np.diag([np.exp(-2 * r)] * 3 + [np.exp(2 * r)] * 3) in_state = squeezed_state(r, phi, basis="fock", fock_dim=cutoff) with eng: ops.Gaussian(cov) | q state = eng.run() assert len(eng.cmd_applied[0]) == 3 for n in range(3): assert np.allclose(state.fidelity(in_state, n), 1, atol=tol)
def test_squeezed(self, setup_eng, cutoff, hbar, tol): eng, prog = setup_eng(3) r = 0.05 phi = 0 cov = (hbar / 2) * np.diag([np.exp(-2 * r)] * 3 + [np.exp(2 * r)] * 3) in_state = squeezed_state(r, phi, basis="fock", fock_dim=cutoff) with prog.context as q: ops.Gaussian(cov) | q state = eng.run(prog).state assert len(eng.run_progs[-1]) == 3 for n in range(3): assert np.allclose(state.fidelity(in_state, n), 1, atol=tol)
def test_vacuum(self, setup_eng, hbar, tol): """Testing a vacuum state""" eng, prog = setup_eng(3) cov = (hbar / 2) * np.identity(6) with prog.context as q: ops.Gaussian(cov, decomp=False) | q state = eng.run(prog).state indices = from_xp(3) cov = cov[:,indices][indices,:] assert np.allclose(state.covs(), np.expand_dims(cov,axis=0), atol=tol) assert np.all(state.means() == np.zeros((1,6))) assert np.allclose(state.fidelity_vacuum(), 1, atol=tol)