def test_two_mode_squeeze_coeff(self, hbar): """Test quadratic coefficients for S2gate""" H, _ = two_mode_squeezing(0.23, hbar=hbar) res, d = quadratic_coefficients(get_quad_operator(H, hbar)) expected = np.fliplr(np.diag([1] * 4)) assert np.allclose(res, expected) assert np.allclose(d, np.zeros([4]))
def test_quad_form(self, hbar): """Test it has the correct form using quadrature operators""" H, _ = two_mode_squeezing(2, mode1=1, mode2=3, hbar=hbar) H = normal_ordered(get_quad_operator(H, hbar=hbar), hbar=hbar) expected = QuadOperator('q1 p3', 1) expected += QuadOperator('p1 q3', 1) assert H == expected
def test_quad_form(self, hbar): """Test it has the correct form using quadrature operators""" H, _ = beamsplitter(np.pi / 4, np.pi / 2, mode1=1, mode2=3, hbar=hbar) H = normal_ordered(get_quad_operator(H, hbar=hbar), hbar=hbar) expected = QuadOperator('q1 q3', -1) expected += QuadOperator('p1 p3', -1) assert H == expected
def test_quad_form(self, hbar): """Test it has the correct form using quadrature operators""" H, _ = squeezing(2, mode=1) H = normal_ordered(get_quad_operator(H, hbar=hbar), hbar=hbar) expected = QuadOperator('q1 p1', -1) expected += QuadOperator('', 1j) assert H == expected
def test_two_mode_squeeze_coeff(self): """Test quadratic coefficients for S2gate""" self.logTestName() H, _ = two_mode_squeezing(0.23, hbar=self.hbar) res, d = quadratic_coefficients(get_quad_operator(H, self.hbar)) expected = np.fliplr(np.diag([1]*4)) self.assertTrue(np.allclose(res, expected)) self.assertTrue(np.allclose(d, np.zeros([4])))
def test_quad_form(self, hbar): """Test it has the correct form using quadrature operators""" H, _ = rotation(self.phi, mode=1, hbar=hbar) H = normal_ordered(get_quad_operator(H, hbar=hbar), hbar=hbar) expected = QuadOperator('q1 q1', -1 / hbar) expected += QuadOperator('p1 p1', -1 / hbar) expected += QuadOperator('', 1) assert H == expected
def test_Dgate_displacement(self, hbar): """Test displacement vector matches Dgate""" a = 0.23 - 0.432j H, t = displacement(a, hbar=hbar) _, d = quadratic_coefficients(get_quad_operator(H, hbar)) expected = np.array([a.real, a.imag]) * np.sqrt(2 * hbar) / t assert np.allclose(d, expected) assert np.allclose(a, t * (d[0] + d[1] * 1j) / np.sqrt(2 * hbar))
def test_rotation_coeff(self, hbar): """Test quadratic coefficients for Rgate""" # one mode H, _ = rotation(0.23, hbar=hbar) res, d = quadratic_coefficients(get_quad_operator(H, hbar)) expected = -np.diag(np.array([1, 1])) assert np.allclose(res, expected) assert np.allclose(d, np.zeros([2])) # two modes H, _ = rotation(0.23, mode=1, hbar=hbar) res, d = quadratic_coefficients(get_quad_operator(H, hbar)) expected = np.zeros([4, 4]) expected[1, 1] = -1 expected[3, 3] = -1 assert np.allclose(res, expected) assert np.allclose(d, np.zeros([4]))
def test_rotation_coeff(self): """Test quadratic coefficients for Rgate""" self.logTestName() # one mode H, _ = rotation(0.23, hbar=self.hbar) res, d = quadratic_coefficients(get_quad_operator(H, self.hbar)) expected = -np.diag(np.array([1, 1])) self.assertTrue(np.allclose(res, expected)) self.assertTrue(np.allclose(d, np.zeros([2]))) # two modes H, _ = rotation(0.23, mode=1, hbar=self.hbar) res, d = quadratic_coefficients(get_quad_operator(H, self.hbar)) expected = np.zeros([4, 4]) expected[1, 1] = -1 expected[3, 3] = -1 self.assertTrue(np.allclose(res, expected)) self.assertTrue(np.allclose(d, np.zeros([4])))
def test_squeeze_coeff(self, hbar): """Test quadratic coefficients for Sgate""" # one mode # pylint: disable=invalid-unary-operand-type H, _ = squeezing(0.23, hbar=hbar) res, d = quadratic_coefficients(get_quad_operator(H, hbar)) expected = -np.array([[0, 1], [1, 0]]) assert np.allclose(res, expected) assert np.allclose(d, np.zeros([2])) # two modes H, _ = squeezing(0.23, mode=1, hbar=hbar) res, d = quadratic_coefficients(get_quad_operator(H, hbar)) expected = np.zeros([4, 4]) expected[1, 3] = -1 expected[3, 1] = -1 assert np.allclose(res, expected) assert np.allclose(d, np.zeros([4]))
def test_quad_form(self): """Test it has the correct form using quadrature operators""" self.logTestName() H, _ = squeezing(2, mode=1) H = normal_ordered(get_quad_operator(H, hbar=self.hbar), hbar=self.hbar) expected = QuadOperator('q1 p1', -1) expected += QuadOperator('', 1j) self.assertEqual(H, expected)
def test_quad_form(self): """Test it has the correct form using quadrature operators""" self.logTestName() H, _ = two_mode_squeezing(2, mode1=1, mode2=3, hbar=self.hbar) H = normal_ordered(get_quad_operator(H, hbar=self.hbar), hbar=self.hbar) expected = QuadOperator('q1 p3', 1) expected += QuadOperator('p1 q3', 1) self.assertEqual(H, expected)
def __init__(self, operator, t=1, mode='local'): super().__init__([t]) if not is_hermitian(operator): raise ValueError("Hamiltonian must be Hermitian.") if mode == 'local': quad_operator = prune_unused_indices(operator) elif mode == 'global': quad_operator = operator if isinstance(quad_operator, BosonOperator): quad_operator = get_quad_operator(quad_operator, hbar=sf.hbar) A, d = quadratic_coefficients(quad_operator) if mode == 'local': self.ns = A.shape[0] // 2 elif mode == 'global': # pylint: disable=protected-access self.ns = pu.Program_current_context.num_subsystems if A.shape[0] < 2 * self.ns: # expand the quadratic coefficient matrix to take # into account the extra modes A_n = A.shape[0] // 2 tmp = np.zeros([2 * self.ns, 2 * self.ns]) tmp[:A_n, :A_n] = A[:A_n, :A_n] tmp[:A_n, self.ns:self.ns + A_n] = A[:A_n, A_n:] tmp[self.ns:self.ns + A_n, :A_n] = A[A_n:, :A_n] tmp[self.ns:self.ns + A_n, self.ns:self.ns + A_n] = A[A_n:, A_n:] A = tmp self.S = expm(sympmat(self.ns) @ A * t) self.disp = False if not np.all(d == 0.): self.disp = True if np.all(A == 0.): self.d = d * t else: if np.linalg.cond(A) >= 1 / sys.float_info.epsilon: # the matrix is singular, add a small epsilon eps = 1e-9 epsI = eps * np.identity(2 * self.ns) s = inv(A + epsI) @ d tmp = (np.identity(2*self.ns) \ - expm(sympmat(self.ns) @ (A+epsI) * t).T) @ s / eps else: s = inv(A) @ d tmp = s - self.S.T @ s self.d = np.zeros([2 * self.ns]) self.d[self.ns:] = tmp[:self.ns] self.d[:self.ns] = tmp[self.ns:]
def test_Dgate_displacement(self): """Test displacement vector matches Dgate""" self.logTestName() a = 0.23-0.432j H, t = displacement(a, hbar=self.hbar) _, d = quadratic_coefficients(get_quad_operator(H, self.hbar)) expected = np.array([a.real, a.imag])*np.sqrt(2*self.hbar)/t self.assertTrue(np.allclose(d, expected)) self.assertTrue(np.allclose(a, t*(d[0]+d[1]*1j)/np.sqrt(2*self.hbar)))
def test_quad_form(self): """Test it has the correct form using quadrature operators""" self.logTestName() H, _ = rotation(self.phi, mode=1, hbar=self.hbar) H = normal_ordered(get_quad_operator(H, hbar=self.hbar), hbar=self.hbar) expected = QuadOperator('q1 q1', -0.5) expected += QuadOperator('p1 p1', -0.5) expected += QuadOperator('', 1) self.assertEqual(H, expected)
def setUp(self): """parameters""" self.hbar = 2. self.eng, _ = sf.Engine(2, hbar=self.hbar) self.J = -1 self.U = 1.5 self.t = 1.086 self.k = 20 self.tol = 1e-2 self.H = bose_hubbard(1, 2, self.J, self.U) self.Hquad = get_quad_operator(self.H, hbar=self.hbar)
def test_quad_form(self): """Test it has the correct form using quadrature operators""" self.logTestName() H, _ = beamsplitter(np.pi / 4, np.pi / 2, mode1=1, mode2=3, hbar=self.hbar) H = normal_ordered(get_quad_operator(H, hbar=self.hbar), hbar=self.hbar) expected = QuadOperator('q1 q3', -1) expected += QuadOperator('p1 p3', -1) self.assertEqual(H, expected)
def test_beamsplitter_coeff(self, hbar): """Test quadratic coefficients for BSgate""" # arbitrary beamsplitter theta = 0.5423 phi = 0.3242 H, _ = beamsplitter(theta, phi, hbar=hbar) res, d = quadratic_coefficients(get_quad_operator(H, hbar=hbar)) expected = np.zeros([4, 4]) expected[0, 3] = expected[3, 0] = -np.cos(np.pi - phi) expected[1, 2] = expected[2, 1] = np.cos(np.pi - phi) expected[0, 1] = expected[1, 0] = -np.sin(np.pi - phi) expected[2, 3] = expected[3, 2] = -np.sin(np.pi - phi) assert np.allclose(res, expected) assert np.allclose(d, np.zeros([4]))
def test_1x2_quadrature_operators(self, eng, tol): """Test a 1x2 lattice Bose-Hubbard model by passing quadrature operators instead of ladder operators""" prog = sf.Program(2) H = get_quad_operator(bose_hubbard(1, 2, self.J, self.U), hbar=2) with prog.context as q: Fock(2) | q[0] BoseHubbardPropagation(H, self.t, self.k) | q state = eng.run(prog).state Hm = -self.J*np.sqrt(2)*np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]]) \ + self.U*np.diag([1, 0, 1]) init_state = np.array([1, 0, 0]) exp = np.abs(np.dot(expm(-1j * self.t * Hm), init_state))**2 assert np.allclose(state.fock_prob([2, 0]), exp[0], rtol=tol) assert np.allclose(state.fock_prob([1, 1]), exp[1], rtol=tol) assert np.allclose(state.fock_prob([0, 2]), exp[2], rtol=tol)
def test_hermitian(self, hbar): """Test output is hermitian""" H, _ = displacement(self.alpha, hbar=hbar) assert is_hermitian(H) assert is_hermitian(get_quad_operator(H))
def test_gaussian(self): """Test output is gaussian""" self.logTestName() H, _ = rotation(self.phi, hbar=self.hbar) res = get_quad_operator(H, hbar=self.hbar).is_gaussian() self.assertTrue(res)
def test_gaussian(self, hbar): """Test output is gaussian""" H, _ = kerr(self.kappa, hbar=hbar) res = get_quad_operator(H).is_gaussian() assert not res
def test_hermitian(self, hbar): """Test output is hermitian""" H, _ = kerr(self.kappa, hbar=hbar) assert is_hermitian(H) assert is_hermitian(get_quad_operator(H))
def test_gaussian(self): """Test output is gaussian""" res = get_quad_operator(self.H).is_gaussian() self.assertTrue(res)
def test_gaussian(self, hbar): """Test output is gaussian""" H, _ = displacement(self.alpha, hbar=hbar) res = get_quad_operator(H, hbar=hbar).is_gaussian() assert res
def test_gaussian(self): """Test output is gaussian""" self.logTestName() H, _ = displacement(self.alpha, hbar=self.hbar) res = get_quad_operator(H, hbar=self.hbar).is_gaussian() self.assertTrue(res)
def test_gaussian(self): """Test output is gaussian""" self.logTestName() res = get_quad_operator(self.H).is_gaussian() self.assertFalse(res)
def test_hermitian(self): """Test output is hermitian""" self.logTestName() self.assertTrue(is_hermitian(self.H)) self.assertTrue(is_hermitian(get_quad_operator(self.H)))
def test_hermitian(self): """Test output is hermitian""" self.logTestName() H, _ = displacement(self.alpha, hbar=self.hbar) self.assertTrue(is_hermitian(H)) self.assertTrue(is_hermitian(get_quad_operator(H)))
def test_hermitian(self): """Test output is hermitian""" self.logTestName() H, _ = rotation(self.phi, hbar=self.hbar) self.assertTrue(is_hermitian(H)) self.assertTrue(is_hermitian(get_quad_operator(H)))