def test_trotterize_noiseless(self): # Noiseless simulations # Compare that the integrator adiabatic results match the trotterized results # First, compare the non-IS subspace results simulation = adiabatic_simulation(sample_graph(), IS_subspace=False) res_trotterize = simulation.performance_vs_total_time( np.arange(1, 5, 1), metric='optimum_overlap', initial_state=State(equal_superposition(6)), schedule=lambda t, tf: simulation.linear_schedule( t, tf, coefficients=[10, 10]), plot=False, verbose=True, method='trotterize') res_RK45 = simulation.performance_vs_total_time( np.arange(1, 5, 1), metric='optimum_overlap', initial_state=State(equal_superposition(6)), schedule=lambda t, tf: simulation.linear_schedule( t, tf, coefficients=[10, 10]), plot=False, verbose=True, method='RK45') # Now test in the IS subspace self.assertTrue( np.allclose(res_trotterize[0]['trotterize']['optimum_overlap'], res_RK45[0]['RK45']['optimum_overlap'], atol=1e-2)) simulation = adiabatic_simulation(sample_graph(), IS_subspace=True) res_trotterize = simulation.performance_vs_total_time( np.arange(1, 4, 1), metric='optimum_overlap', schedule=lambda t, tf: simulation.linear_schedule( t, tf, coefficients=[10, 10]), plot=False, verbose=True, method='trotterize') res_RK45 = simulation.performance_vs_total_time( np.arange(1, 4, 1), metric='optimum_overlap', schedule=lambda t, tf: simulation.linear_schedule( t, tf, coefficients=[10, 10]), plot=False, verbose=True, method='RK45') self.assertTrue( np.allclose(res_trotterize[0]['trotterize']['optimum_overlap'], res_RK45[0]['RK45']['optimum_overlap'], atol=1e-2))
def test_hamiltonian_driver(self): N = 6 hl_qubit = hamiltonian.HamiltonianDriver(graph=tools_test.sample_graph()) psi0 = State(np.zeros((2 ** N, 1))) psi0[0, 0] = 1 psi1 = State(tools.outer_product(psi0, psi0)) # Evolve by e^{-i (\pi/2) \sum_i X_i} psi0 = hl_qubit.evolve(psi0, np.pi / 2) # Should get (-1j)^N |111111> self.assertTrue(np.vdot(psi0, psi0) == 1) self.assertTrue(psi0[-1, 0] == (-1j) ** N) # Evolve by e^{-i (\pi/2) \sum_i X_i} psi1 = hl_qubit.evolve(psi1, np.pi / 2) # Should get (-1j)^N |111111> self.assertTrue(tools.is_valid_state(psi1)) self.assertAlmostEqual(psi1[-1, -1], 1) psi0 = State(np.zeros((2 ** N, 1))) psi0[0, 0] = 1 psi0 = hl_qubit.left_multiply(psi0) psi1 = np.zeros((2 ** N, 1), dtype=np.complex128) for i in range(N): psi1[2 ** i, 0] = 1 self.assertTrue(np.allclose(psi0, psi1)) psi2 = State(np.zeros((2 ** N, 1))) psi2[0, 0] = 1 psi2 = hl_qubit.hamiltonian @ psi2 self.assertTrue(np.allclose(psi0, psi2)) N = 3 hl = hamiltonian.HamiltonianDriver(transition=(0, 1), code=rydberg) psi0 = State(np.zeros((rydberg.d ** N, 1)), code=rydberg) psi0[5, 0] = 1 psi1 = State(tools.outer_product(psi0, psi0), code=rydberg) psi0 = hl.left_multiply(psi0) self.assertTrue(psi0[2, 0] == 1) self.assertTrue(psi0[14, 0] == 1) psi1 = hl.left_multiply(psi1) self.assertTrue(psi1[2, 5] == 1) self.assertTrue(psi1[14, 5] == 1) psi0 = State(np.zeros((rydberg.d ** N, 1)), code=rydberg) psi0[5, 0] = 1 psi0 = hl.evolve(psi0, np.pi / 2) self.assertTrue(np.isclose(psi0[11, 0], -1)) # IS subspace hl = hamiltonian.HamiltonianDriver(transition=(0, 2), code=rydberg, IS_subspace=True, graph=line_graph(2)) psi0 = State(np.zeros((8, 1)), code=rydberg) psi0[-1,0] = 1 psi0 = State(tools.outer_product(psi0, psi0), code=rydberg) self.assertTrue(tools.is_hermitian(hl.evolve(psi0, 1)))
def test_IS_subspace(self): g = tools_test.sample_graph() self.assertTrue(g.num_independent_sets == 13) self.assertTrue(g.mis_size == 2)
import numpy as np import unittest from qsim.graph_algorithms.graph import line_graph from qsim import tools from qsim.evolution import hamiltonian from qsim.codes import rydberg from qsim.test import tools_test from qsim.codes.quantum_state import State # Generate sample graph g = tools_test.sample_graph() class TestHamiltonian(unittest.TestCase): def test_hamiltonian_C(self): pass # Then compute MaxCut energies hc = hamiltonian.HamiltonianMaxCut(g) self.assertTrue(hc.hamiltonian[0, 0] == 0) self.assertTrue(hc.hamiltonian[-1, -1] == 0) self.assertTrue(hc.hamiltonian[2, 2] == 3) def test_rydberg_hamiltonian(self): # Test normal MIS Hamiltonian # Graph has six nodes and nine edges hc = hamiltonian.HamiltonianMIS(g) self.assertTrue(hc.hamiltonian[0, 0] == -3) self.assertTrue(hc.hamiltonian[-1, -1] == 0) self.assertTrue(hc.hamiltonian[-2, -2] == 1)
import numpy as np import unittest from qsim.test.tools_test import sample_graph from qsim.tools.tools import equal_superposition, outer_product from qsim.graph_algorithms.graph import Graph, ring_graph from qsim.codes.quantum_state import State import networkx as nx from qsim.evolution import quantum_channels, hamiltonian from qsim.graph_algorithms import qaoa from qsim.codes import two_qubit_code, jordan_farhi_shor # Generate sample graph N = 6 g = sample_graph() ring = ring_graph(N) hc = hamiltonian.HamiltonianMaxCut(g) hc_ring = hamiltonian.HamiltonianMaxCut(ring) hb = hamiltonian.HamiltonianDriver() hamiltonians = [hc, hb] ring_hamiltonians = [hc_ring, hb] sim = qaoa.SimulateQAOA(g, cost_hamiltonian=hc, hamiltonian=hamiltonians) sim_ring = qaoa.SimulateQAOA(ring, cost_hamiltonian=hc_ring, hamiltonian=ring_hamiltonians) sim_ket = qaoa.SimulateQAOA(g, cost_hamiltonian=hc, hamiltonian=hamiltonians) sim_noisy = qaoa.SimulateQAOA(g, cost_hamiltonian=hc,