def test_large_eigenvalue(self): E_fci = -20 coef_vec = [E_fci, 0] circ_vec = [ Circuit( ), build_circuit('Z_1') ] H2_qubit_hamiltonian = QubitOperator() for i in range(len(circ_vec)): H2_qubit_hamiltonian.add(coef_vec[i], circ_vec[i]) ref = [1,0] print('\nBegin QPE test') print('----------------------') # make test with algorithm class mol = Molecule() mol.hamiltonian = H2_qubit_hamiltonian alg = QPE(mol, reference=ref, trotter_number=2) alg.run(guess_energy = E_fci, t = 1, nruns = 50, success_prob = 0.9, num_precise_bits = 8) Egs = alg.get_gs_energy() assert Egs == approx(E_fci, abs=1.1e-3)
def test_trotterization(self): circ_vec = [Circuit(), build_circuit('Z_0')] coef_vec = [-1.0j * 0.5, -1.0j * -0.04544288414432624] # the operator to be exponentiated minus_iH = QubitOperator() for i in range(len(circ_vec)): minus_iH.add(coef_vec[i], circ_vec[i]) # exponentiate the operator Utrot, phase = trotterization.trotterize(minus_iH) inital_state = np.zeros(2**4, dtype=complex) inital_state[3] = np.sqrt(2 / 3) inital_state[12] = -np.sqrt(1 / 3) # initalize a quantum computer with above coeficients # i.e. ca|1100> + cb|0011> qc = Computer(4) qc.set_coeff_vec(inital_state) # apply the troterized minus_iH qc.apply_circuit(Utrot) qc.apply_constant(phase) smart_print(qc) coeffs = qc.get_coeff_vec() assert np.real(coeffs[3]) == approx(0.6980209737879599, abs=1.0e-15) assert np.imag(coeffs[3]) == approx(-0.423595782342996, abs=1.0e-15) assert np.real(coeffs[12]) == approx(-0.5187235657531178, abs=1.0e-15) assert np.imag(coeffs[12]) == approx(0.25349397560041553, abs=1.0e-15)
def test_H2_qpe(self): print('\n'), # The FCI energy for H2 at 1.5 Angstrom in a sto-3g basis E_fci = -0.9981493534 coef_vec = [-0.4917857774144603, 0.09345649662931771, 0.09345649662931771, -0.0356448161226769, -0.0356448161226769, 0.1381758457453024, 0.05738398402634884, -0.0573839840263488, -0.0573839840263488, 0.05738398402634884, 0.08253705485911705, 0.13992103888546592, 0.13992103888546592, 0.08253705485911705, 0.1458551902800438] circ_vec = [ Circuit( ), build_circuit( 'Z_0' ), build_circuit( 'Z_1' ), build_circuit( 'Z_2' ), build_circuit( 'Z_3' ), build_circuit( 'Z_0 Z_1' ), build_circuit( 'Y_0 X_1 X_2 Y_3' ), build_circuit( 'X_0 X_1 Y_2 Y_3' ), build_circuit( 'Y_0 Y_1 X_2 X_3' ), build_circuit( 'X_0 Y_1 Y_2 X_3' ), build_circuit( 'Z_0 Z_2' ), build_circuit( 'Z_0 Z_3' ), build_circuit( 'Z_1 Z_2' ), build_circuit( 'Z_1 Z_3' ), build_circuit( 'Z_2 Z_3' )] H2_qubit_hamiltonian = QubitOperator() for i in range(len(circ_vec)): H2_qubit_hamiltonian.add(coef_vec[i], circ_vec[i]) ref = [1,1,0,0] print('\nBegin QPE test for H2') print('----------------------') # make test with algorithm class mol = Molecule() mol.hamiltonian = H2_qubit_hamiltonian alg = QPE(mol, reference=ref, trotter_number=2) alg.run(guess_energy = -1, t = 0.4, nruns = 100, success_prob = 0.5, num_precise_bits = 8) Egs = alg.get_gs_energy() assert Egs == approx(E_fci, abs=1.1e-3)
def test_trotterization_with_controlled_U(self): circ_vec = [build_circuit('Y_0 X_1'), build_circuit('X_0 Y_1')] coef_vec = [-1.0719145972781818j, 1.0719145972781818j] # the operator to be exponentiated minus_iH = QubitOperator() for i in range(len(circ_vec)): minus_iH.add(coef_vec[i], circ_vec[i]) ancilla_idx = 2 # exponentiate the operator Utrot, phase = trotterization.trotterize_w_cRz(minus_iH, ancilla_idx) # Case 1: positive control # initalize a quantum computer qc = Computer(3) # build HF state qc.apply_gate(gate('X', 0, 0)) # put ancilla in |1> state qc.apply_gate(gate('X', 2, 2)) # apply the troterized minus_iH qc.apply_circuit(Utrot) smart_print(qc) coeffs = qc.get_coeff_vec() assert coeffs[5] == approx(-0.5421829373021542, abs=1.0e-15) assert coeffs[6] == approx(-0.8402604730072732, abs=1.0e-15) # Case 2: negitive control # initalize a quantum computer qc = Computer(3) # build HF state qc.apply_gate(gate('X', 0, 0)) # apply the troterized minus_iH qc.apply_circuit(Utrot) smart_print(qc) coeffs = qc.get_coeff_vec() assert coeffs[1] == approx(1, abs=1.0e-15)
def test_H2_experiment_perfect(self): print('\n') #the RHF H2 energy at equilibrium bond length E_hf = -1.1166843870661929 #the H2 qubit hamiltonian circ_vec = [ Circuit(), build_circuit('Z_0'), build_circuit('Z_1'), build_circuit('Z_2'), build_circuit('Z_3'), build_circuit('Z_0 Z_1'), build_circuit('Y_0 X_1 X_2 Y_3'), build_circuit('Y_0 Y_1 X_2 X_3'), build_circuit('X_0 X_1 Y_2 Y_3'), build_circuit('X_0 Y_1 Y_2 X_3'), build_circuit('Z_0 Z_2'), build_circuit('Z_0 Z_3'), build_circuit('Z_1 Z_2'), build_circuit('Z_1 Z_3'), build_circuit('Z_2 Z_3') ] coef_vec = [ -0.098863969784274, 0.1711977489805748, 0.1711977489805748, -0.222785930242875, -0.222785930242875, 0.1686221915724993, 0.0453222020577776, -0.045322202057777, -0.045322202057777, 0.0453222020577776, 0.1205448220329002, 0.1658670240906778, 0.1658670240906778, 0.1205448220329002, 0.1743484418396386 ] H2_qubit_hamiltonian = QubitOperator() for i in range(len(circ_vec)): H2_qubit_hamiltonian.add(coef_vec[i], circ_vec[i]) # circuit for making HF state circ = Circuit() circ.add(gate('X', 0, 0)) circ.add(gate('X', 1, 1)) TestExperiment = Experiment(4, circ, H2_qubit_hamiltonian, 1000000) params2 = [] avg_energy = TestExperiment.perfect_experimental_avg(params2) print('Perfectly Measured H2 Experimental Avg. Energy') print(avg_energy) print('H2 RHF Energy') print(E_hf) experimental_error = abs(avg_energy - E_hf) assert experimental_error == approx(0, abs=1.0e-14)
def test_op_exp_val_1(self): # test direct expectation value measurement trial_state = Computer(4) trial_prep = [None] * 5 trial_prep[0] = gate('H', 0, 0) trial_prep[1] = gate('H', 1, 1) trial_prep[2] = gate('H', 2, 2) trial_prep[3] = gate('H', 3, 3) trial_prep[4] = gate('cX', 0, 1) trial_circ = Circuit() #prepare the circuit for gate_ in trial_prep: trial_circ.add(gate_) # use circuit to prepare trial state trial_state.apply_circuit(trial_circ) # gates needed for [a1^ a2] operator X1 = gate('X', 1, 1) X2 = gate('X', 2, 2) Y1 = gate('Y', 1, 1) Y2 = gate('Y', 2, 2) # initialize circuits to make operator circ1 = Circuit() circ1.add(X2) circ1.add(Y1) circ2 = Circuit() circ2.add(Y2) circ2.add(Y1) circ3 = Circuit() circ3.add(X2) circ3.add(X1) circ4 = Circuit() circ4.add(Y2) circ4.add(X1) #build the quantum operator for [a1^ a2] a1_dag_a2 = QubitOperator() a1_dag_a2.add(0.0 - 0.25j, circ1) a1_dag_a2.add(0.25, circ2) a1_dag_a2.add(0.25, circ3) a1_dag_a2.add(0.0 + 0.25j, circ4) #get direct expectatoin value exp = trial_state.direct_op_exp_val(a1_dag_a2) assert exp == approx(0.25, abs=2.0e-16)
def test_jw2(self): coeff_vec = [ -0.25j, -0.25, 0.25, -0.25j, -0.125j, 0.125, -0.125, -0.125j, -0.125, -0.125j, 0.125j, -0.125 ] circ_vec = [ build_circuit(x) for x in [ "Y_2 X_3", "Y_2 Y_3", "X_2 X_3", "X_2 Y_3", "Z_0 X_1 Y_3 X_4", "Z_0 X_1 Y_3 Y_4", "Z_0 X_1 X_3 X_4", "Z_0 X_1 X_3 Y_4", "Z_0 Y_1 Y_3 X_4", "Z_0 Y_1 Y_3 Y_4", "Z_0 Y_1 X_3 X_4", "Z_0 Y_1 X_3 Y_4" ] ] correct_op = QubitOperator() for coeff, circ in zip(coeff_vec, circ_vec): correct_op.add(coeff, circ) a = SQOperator() a.add(1.00, [2, 3], []) a.add(1.00, [1], [3, 4]) aop = a.jw_transform() assert aop.check_op_equivalence(correct_op, True) is True
def test_H4_fast_qkd(self): print('\n') # The FCI energy for H4 at 1.5 Angstrom in a sto-6g basis E_fci = -2.0126741263939656 # The He hamilitonian circ_vec = [ Circuit(), build_circuit('Z_0'), build_circuit('Y_0 Z_1 Z_2 Z_3 Y_4'), build_circuit('X_0 Z_1 Z_2 Z_3 X_4'), build_circuit('Z_1'), build_circuit('Y_1 Z_2 Z_3 Z_4 Y_5'), build_circuit('X_1 Z_2 Z_3 Z_4 X_5'), build_circuit('Z_2'), build_circuit('Y_2 Z_3 Z_4 Z_5 Y_6'), build_circuit('X_2 Z_3 Z_4 Z_5 X_6'), build_circuit('Z_3'), build_circuit('Y_3 Z_4 Z_5 Z_6 Y_7'), build_circuit('X_3 Z_4 Z_5 Z_6 X_7'), build_circuit('Z_4'), build_circuit('Z_5'), build_circuit('Z_6'), build_circuit('Z_7'), build_circuit('Z_0 Z_1'), build_circuit('Y_0 Z_2 Z_3 Y_4'), build_circuit('X_0 Z_2 Z_3 X_4'), build_circuit('Y_0 X_1 X_2 Y_3'), build_circuit('X_0 X_1 Y_2 Y_3'), build_circuit('Y_0 Y_1 X_2 X_3'), build_circuit('X_0 Y_1 Y_2 X_3'), build_circuit('Y_0 X_1 X_3 Z_4 Z_5 Y_6'), build_circuit('X_0 X_1 X_3 Z_4 Z_5 X_6'), build_circuit('Y_0 Y_1 Y_3 Z_4 Z_5 Y_6'), build_circuit('X_0 Y_1 Y_3 Z_4 Z_5 X_6'), build_circuit('Z_0 Y_1 Z_2 Z_3 Z_4 Y_5'), build_circuit('Z_0 X_1 Z_2 Z_3 Z_4 X_5'), build_circuit('Y_0 X_1 X_4 Y_5'), build_circuit('X_0 X_1 Y_4 Y_5'), build_circuit('Y_0 Y_1 X_4 X_5'), build_circuit('X_0 Y_1 Y_4 X_5'), build_circuit('Y_0 X_1 X_2 Z_3 Z_4 Z_5 Z_6 Y_7'), build_circuit('X_0 X_1 Y_2 Z_3 Z_4 Z_5 Z_6 Y_7'), build_circuit('Y_0 Y_1 X_2 Z_3 Z_4 Z_5 Z_6 X_7'), build_circuit('X_0 Y_1 Y_2 Z_3 Z_4 Z_5 Z_6 X_7'), build_circuit('Y_0 X_1 X_6 Y_7'), build_circuit('X_0 X_1 Y_6 Y_7'), build_circuit('Y_0 Y_1 X_6 X_7'), build_circuit('X_0 Y_1 Y_6 X_7'), build_circuit('Z_0 Z_2'), build_circuit('Z_0 Y_2 Z_3 Z_4 Z_5 Y_6'), build_circuit('Z_0 X_2 Z_3 Z_4 Z_5 X_6'), build_circuit('Y_0 Z_1 Z_3 Y_4'), build_circuit('X_0 Z_1 Z_3 X_4'), build_circuit('Y_0 Z_1 X_2 X_4 Z_5 Y_6'), build_circuit('X_0 Z_1 X_2 X_4 Z_5 X_6'), build_circuit('X_0 Z_1 X_2 Y_4 Z_5 Y_6'), build_circuit('Y_0 Z_1 Y_2 X_4 Z_5 X_6'), build_circuit('Y_0 Z_1 Y_2 Y_4 Z_5 Y_6'), build_circuit('X_0 Z_1 Y_2 Y_4 Z_5 X_6'), build_circuit('Z_0 Z_3'), build_circuit('Y_0 Z_1 Z_2 Y_4'), build_circuit('X_0 Z_1 Z_2 X_4'), build_circuit('Y_0 Z_1 Y_2 Y_3 Z_4 Y_5'), build_circuit('X_0 Z_1 X_2 Y_3 Z_4 Y_5'), build_circuit('Y_0 Z_1 Y_2 X_3 Z_4 X_5'), build_circuit('X_0 Z_1 X_2 X_3 Z_4 X_5'), build_circuit('Y_0 Z_1 Z_2 X_3 X_5 Y_6'), build_circuit('X_0 Z_1 Z_2 X_3 X_5 X_6'), build_circuit('Y_0 Z_1 Z_2 Y_3 Y_5 Y_6'), build_circuit('X_0 Z_1 Z_2 Y_3 Y_5 X_6'), build_circuit('Z_0 Y_3 Z_4 Z_5 Z_6 Y_7'), build_circuit('Z_0 X_3 Z_4 Z_5 Z_6 X_7'), build_circuit('Y_0 Z_1 Z_2 X_3 X_4 Z_5 Z_6 Y_7'), build_circuit('X_0 Z_1 Z_2 X_3 Y_4 Z_5 Z_6 Y_7'), build_circuit('Y_0 Z_1 Z_2 Y_3 X_4 Z_5 Z_6 X_7'), build_circuit('X_0 Z_1 Z_2 Y_3 Y_4 Z_5 Z_6 X_7'), build_circuit('Z_0 Z_4'), build_circuit('Z_0 Z_5'), build_circuit('Y_0 Z_1 Z_2 Z_3 Y_4 Z_5'), build_circuit('X_0 Z_1 Z_2 Z_3 X_4 Z_5'), build_circuit('Y_0 Z_1 Y_2 Y_5 Z_6 Y_7'), build_circuit('X_0 Z_1 X_2 Y_5 Z_6 Y_7'), build_circuit('Y_0 Z_1 Y_2 X_5 Z_6 X_7'), build_circuit('X_0 Z_1 X_2 X_5 Z_6 X_7'), build_circuit('Y_0 Z_1 Z_2 Z_3 Z_4 X_5 X_6 Y_7'), build_circuit('X_0 Z_1 Z_2 Z_3 Z_4 X_5 Y_6 Y_7'), build_circuit('Y_0 Z_1 Z_2 Z_3 Z_4 Y_5 X_6 X_7'), build_circuit('X_0 Z_1 Z_2 Z_3 Z_4 Y_5 Y_6 X_7'), build_circuit('Z_0 Z_6'), build_circuit('Y_0 Z_1 Z_2 Z_3 Y_4 Z_6'), build_circuit('X_0 Z_1 Z_2 Z_3 X_4 Z_6'), build_circuit('Z_0 Z_7'), build_circuit('Y_0 Z_1 Z_2 Z_3 Y_4 Z_7'), build_circuit('X_0 Z_1 Z_2 Z_3 X_4 Z_7'), build_circuit('Z_1 Z_2'), build_circuit('Y_1 Z_3 Z_4 Y_5'), build_circuit('X_1 Z_3 Z_4 X_5'), build_circuit('Y_1 X_2 X_3 Y_4'), build_circuit('X_1 X_2 Y_3 Y_4'), build_circuit('Y_1 Y_2 X_3 X_4'), build_circuit('X_1 Y_2 Y_3 X_4'), build_circuit('Y_1 X_2 X_4 Z_5 Z_6 Y_7'), build_circuit('X_1 X_2 X_4 Z_5 Z_6 X_7'), build_circuit('Y_1 Y_2 Y_4 Z_5 Z_6 Y_7'), build_circuit('X_1 Y_2 Y_4 Z_5 Z_6 X_7'), build_circuit('Z_1 Y_2 Z_3 Z_4 Z_5 Y_6'), build_circuit('Z_1 X_2 Z_3 Z_4 Z_5 X_6'), build_circuit('Y_1 X_2 X_5 Y_6'), build_circuit('X_1 X_2 Y_5 Y_6'), build_circuit('Y_1 Y_2 X_5 X_6'), build_circuit('X_1 Y_2 Y_5 X_6'), build_circuit('Z_1 Z_3'), build_circuit('Z_1 Y_3 Z_4 Z_5 Z_6 Y_7'), build_circuit('Z_1 X_3 Z_4 Z_5 Z_6 X_7'), build_circuit('Y_1 Z_2 Z_4 Y_5'), build_circuit('X_1 Z_2 Z_4 X_5'), build_circuit('Y_1 Z_2 X_3 X_5 Z_6 Y_7'), build_circuit('X_1 Z_2 X_3 X_5 Z_6 X_7'), build_circuit('X_1 Z_2 X_3 Y_5 Z_6 Y_7'), build_circuit('Y_1 Z_2 Y_3 X_5 Z_6 X_7'), build_circuit('Y_1 Z_2 Y_3 Y_5 Z_6 Y_7'), build_circuit('X_1 Z_2 Y_3 Y_5 Z_6 X_7'), build_circuit('Z_1 Z_4'), build_circuit('Y_1 Z_2 Z_3 Y_5'), build_circuit('X_1 Z_2 Z_3 X_5'), build_circuit('Y_1 Z_2 Y_3 Y_4 Z_5 Y_6'), build_circuit('X_1 Z_2 X_3 Y_4 Z_5 Y_6'), build_circuit('Y_1 Z_2 Y_3 X_4 Z_5 X_6'), build_circuit('X_1 Z_2 X_3 X_4 Z_5 X_6'), build_circuit('Y_1 Z_2 Z_3 X_4 X_6 Y_7'), build_circuit('X_1 Z_2 Z_3 X_4 X_6 X_7'), build_circuit('Y_1 Z_2 Z_3 Y_4 Y_6 Y_7'), build_circuit('X_1 Z_2 Z_3 Y_4 Y_6 X_7'), build_circuit('Z_1 Z_5'), build_circuit('Z_1 Z_6'), build_circuit('Y_1 Z_2 Z_3 Z_4 Y_5 Z_6'), build_circuit('X_1 Z_2 Z_3 Z_4 X_5 Z_6'), build_circuit('Z_1 Z_7'), build_circuit('Y_1 Z_2 Z_3 Z_4 Y_5 Z_7'), build_circuit('X_1 Z_2 Z_3 Z_4 X_5 Z_7'), build_circuit('Z_2 Z_3'), build_circuit('Y_2 Z_4 Z_5 Y_6'), build_circuit('X_2 Z_4 Z_5 X_6'), build_circuit('Y_2 X_3 X_4 Y_5'), build_circuit('X_2 X_3 Y_4 Y_5'), build_circuit('Y_2 Y_3 X_4 X_5'), build_circuit('X_2 Y_3 Y_4 X_5'), build_circuit('Z_2 Y_3 Z_4 Z_5 Z_6 Y_7'), build_circuit('Z_2 X_3 Z_4 Z_5 Z_6 X_7'), build_circuit('Y_2 X_3 X_6 Y_7'), build_circuit('X_2 X_3 Y_6 Y_7'), build_circuit('Y_2 Y_3 X_6 X_7'), build_circuit('X_2 Y_3 Y_6 X_7'), build_circuit('Z_2 Z_4'), build_circuit('Y_2 Z_3 Z_5 Y_6'), build_circuit('X_2 Z_3 Z_5 X_6'), build_circuit('Z_2 Z_5'), build_circuit('Y_2 Z_3 Z_4 Y_6'), build_circuit('X_2 Z_3 Z_4 X_6'), build_circuit('Y_2 Z_3 Y_4 Y_5 Z_6 Y_7'), build_circuit('X_2 Z_3 X_4 Y_5 Z_6 Y_7'), build_circuit('Y_2 Z_3 Y_4 X_5 Z_6 X_7'), build_circuit('X_2 Z_3 X_4 X_5 Z_6 X_7'), build_circuit('Z_2 Z_6'), build_circuit('Z_2 Z_7'), build_circuit('Y_2 Z_3 Z_4 Z_5 Y_6 Z_7'), build_circuit('X_2 Z_3 Z_4 Z_5 X_6 Z_7'), build_circuit('Z_3 Z_4'), build_circuit('Y_3 Z_5 Z_6 Y_7'), build_circuit('X_3 Z_5 Z_6 X_7'), build_circuit('Y_3 X_4 X_5 Y_6'), build_circuit('X_3 X_4 Y_5 Y_6'), build_circuit('Y_3 Y_4 X_5 X_6'), build_circuit('X_3 Y_4 Y_5 X_6'), build_circuit('Z_3 Z_5'), build_circuit('Y_3 Z_4 Z_6 Y_7'), build_circuit('X_3 Z_4 Z_6 X_7'), build_circuit('Z_3 Z_6'), build_circuit('Y_3 Z_4 Z_5 Y_7'), build_circuit('X_3 Z_4 Z_5 X_7'), build_circuit('Z_3 Z_7'), build_circuit('Z_4 Z_5'), build_circuit('Y_4 X_5 X_6 Y_7'), build_circuit('X_4 X_5 Y_6 Y_7'), build_circuit('Y_4 Y_5 X_6 X_7'), build_circuit('X_4 Y_5 Y_6 X_7'), build_circuit('Z_4 Z_6'), build_circuit('Z_4 Z_7'), build_circuit('Z_5 Z_6'), build_circuit('Z_5 Z_7'), build_circuit('Z_6 Z_7')] coef_vec = [ -0.9398866173443505, 0.12129073807829349, 0.00641125283150221, 0.00641125283150221, 0.12129073807829353, 0.0064112528315022135, 0.0064112528315022135, 0.07308734612725618, -0.010595295586558178, -0.010595295586558178, 0.0730873461272563, -0.010595295586558171, -0.010595295586558171, -0.004903180263267734, -0.0049031802632676785, -0.0972417632017775, -0.09724176320177753, 0.10115751743776852, 0.016853161923367253, 0.016853161923367253, 0.03977035285263866, -0.03977035285263866, -0.03977035285263866, 0.03977035285263866, 0.00905981792020611, 0.00905981792020611, 0.00905981792020611, 0.00905981792020611, 0.016853161923367253, 0.016853161923367253, 0.028827330860814942, -0.028827330860814942, -0.028827330860814942, 0.028827330860814942, -0.00905981792020611, 0.00905981792020611, 0.00905981792020611, -0.00905981792020611, 0.027456714140683562, -0.027456714140683562, -0.027456714140683562, 0.027456714140683562, 0.05019794254014193, 0.008400644724214635, 0.008400644724214635, 0.01678679876377047, 0.01678679876377047, 0.008370670304957601, 0.019986723846961468, 0.011616053542003863, 0.011616053542003863, 0.019986723846961468, 0.008370670304957601, 0.08996829539278059, -0.00401664635059683, -0.00401664635059683, 0.020803445114367297, 0.020803445114367297, 0.020803445114367297, 0.020803445114367297, -0.020045295426307817, -0.020045295426307817, -0.020045295426307817, -0.020045295426307817, 0.01746046264442075, 0.01746046264442075, 0.028415965731265418, -0.028415965731265418, -0.028415965731265418, 0.028415965731265418, 0.062364797002734416, 0.09119212786354935, -0.0029292718691351837, -0.0029292718691351837, 0.040032019273269284, 0.040032019273269284, 0.040032019273269284, 0.040032019273269284, -0.008895760438436422, 0.008895760438436422, 0.008895760438436422, -0.008895760438436422, 0.077813400984472, 0.008622786573744252, 0.008622786573744252, 0.10527011512515555, 0.017518547012180676, 0.017518547012180676, 0.08996829539278059, -0.00401664635059683, -0.00401664635059683, 0.020803445114367297, -0.020803445114367297, -0.020803445114367297, 0.020803445114367297, -0.020045295426307817, -0.020045295426307817, -0.020045295426307817, -0.020045295426307817, 0.01746046264442075, 0.01746046264442075, 0.028415965731265418, -0.028415965731265418, -0.028415965731265418, 0.028415965731265418, 0.05019794254014193, 0.008400644724214635, 0.008400644724214635, 0.01678679876377047, 0.01678679876377047, 0.008370670304957601, 0.019986723846961468, 0.011616053542003863, 0.011616053542003863, 0.019986723846961468, 0.008370670304957601, 0.09119212786354935, -0.0029292718691351837, -0.0029292718691351837, 0.040032019273269284, 0.040032019273269284, 0.040032019273269284, 0.040032019273269284, 0.008895760438436422, 0.008895760438436422, 0.008895760438436422, 0.008895760438436422, 0.062364797002734416, 0.10527011512515555, 0.017518547012180676, 0.017518547012180676, 0.077813400984472, 0.008622786573744252, 0.008622786573744252, 0.09416046345893181, -0.0025909758017290006, -0.0025909758017290006, 0.035233425879700006, -0.035233425879700006, -0.035233425879700006, 0.035233425879700006, -0.0025909758017290006, -0.0025909758017290006, 0.029476085351951252, -0.029476085351951252, -0.029476085351951252, 0.029476085351951252, 0.05898073895378817, 0.018463477774636035, 0.018463477774636035, 0.09421416483348818, -0.003276374161489622, -0.003276374161489622, 0.02173985193612566, 0.02173985193612566, 0.02173985193612566, 0.02173985193612566, 0.06484557443787466, 0.0943216597898259, 0.01867832322072119, 0.01867832322072119, 0.09421416483348818, -0.003276374161489622, -0.003276374161489622, 0.02173985193612566, -0.02173985193612566, -0.02173985193612566, 0.02173985193612566, 0.05898073895378817, 0.018463477774636035, 0.018463477774636035, 0.0943216597898259, 0.01867832322072119, 0.01867832322072119, 0.06484557443787466, 0.0970891982291751, 0.042405629926062886, -0.042405629926062886, -0.042405629926062886, 0.042405629926062886, 0.05395287215237218, 0.09635850207843506, 0.09635850207843506, 0.05395287215237218, 0.11278693858600855] H4_qubit_hamiltonian = QubitOperator() for i in range(len(circ_vec)): H4_qubit_hamiltonian.add(coef_vec[i], circ_vec[i]) ref = [1,1,1,1,0,0,0,0] # make test with algorithm class mol = Molecule() mol.hamiltonian = H4_qubit_hamiltonian # WARNING! With large s, these methods can become numerically unstable. # Near 0 eigenvalues in the overlap matrices lead to unstable eigenvectors. # MRSQK alg2 = MRSQK(mol, reference=ref, trotter_number=100) alg2.run(s=3, d=3) Egs2 = alg2.get_gs_energy() assert Egs2 == approx(E_fci, abs=1.0e-6) # SRQK alg1 = SRQK(mol, reference=ref, trotter_number=100) alg1.run(s=6) Egs1 = alg1.get_gs_energy() assert Egs1 == approx(E_fci, abs=1.0e-4) # Non-Trotterized (exact) SRQK alg0 = NTSRQK(mol, reference=ref, verbose=True) alg0.run(s=4, dt = 0.8) Egs0 = alg0.get_gs_energy() assert Egs0 == approx(E_fci, abs=1.0e-3)
def test_sparse_operator(self): """ test the SparseMatrix and SparseVector classes """ coeff_vec = [ -0.093750, +0.093750j, -0.093750, -0.093750j, -0.093750, -0.093750j, +0.062500j, -0.093750, -0.093750, +0.093750j, +0.093750, -0.062500, -0.093750j, -0.062500j, +0.062500j, -0.062500, +0.062500, +0.062500, -0.062500j, -0.093750, +0.062500j, -0.062500, -0.062500j, -0.062500, +0.093750j, +0.093750j, +0.062500j, +0.093750, -0.062500, -0.062500, -0.093750j, -0.062500j ] circ_vec = [ build_circuit('X_1 Y_2 X_3 Y_4'), build_circuit('X_1 Y_2 X_3 X_4'), build_circuit('X_1 Y_2 Y_3 X_4'), build_circuit('X_1 X_2 X_3 Y_4'), build_circuit('X_1 X_2 X_3 X_4'), build_circuit('X_1 X_2 Y_3 X_4'), build_circuit('Y_2 X_3 X_4 Z_5 X_6'), build_circuit('Y_1 Y_2 Y_3 Y_4'), build_circuit('Y_1 X_2 X_3 Y_4'), build_circuit('Y_1 X_2 X_3 X_4'), build_circuit('Y_1 Y_2 X_3 X_4'), build_circuit('X_2 X_3 X_4 Z_5 X_6'), build_circuit('Y_1 X_2 Y_3 Y_4'), build_circuit('X_2 Y_3 Y_4 Z_5 Y_6'), build_circuit('X_2 Y_3 X_4 Z_5 X_6'), build_circuit('X_2 Y_3 Y_4 Z_5 X_6'), build_circuit('X_2 X_3 Y_4 Z_5 Y_6'), build_circuit('Y_2 Y_3 X_4 Z_5 X_6'), build_circuit('X_2 X_3 Y_4 Z_5 X_6'), build_circuit('Y_1 X_2 Y_3 X_4'), build_circuit('Y_2 Y_3 X_4 Z_5 Y_6'), build_circuit('Y_2 X_3 X_4 Z_5 Y_6'), build_circuit('X_2 X_3 X_4 Z_5 Y_6'), build_circuit('X_2 Y_3 X_4 Z_5 Y_6'), build_circuit('Y_1 Y_2 Y_3 X_4'), build_circuit('Y_1 Y_2 X_3 Y_4'), build_circuit('Y_2 Y_3 Y_4 Z_5 X_6'), build_circuit('X_1 X_2 Y_3 Y_4'), build_circuit('Y_2 Y_3 Y_4 Z_5 Y_6'), build_circuit('Y_2 X_3 Y_4 Z_5 X_6'), build_circuit('X_1 Y_2 Y_3 Y_4'), build_circuit('Y_2 X_3 Y_4 Z_5 Y_6') ] qubit_op = QubitOperator() for coeff, circ in zip(coeff_vec, circ_vec): qubit_op.add(coeff, circ) num_qb = qubit_op.num_qubits() qci = Computer(num_qb) arb_vec = np.linspace(0, 2 * np.pi, 2**num_qb) arb_vec = arb_vec / np.linalg.norm(arb_vec) qci.set_coeff_vec(arb_vec) sp_mat_op = qubit_op.sparse_matrix(num_qb) ci = np.array(qci.get_coeff_vec()) qci.apply_operator(qubit_op) diff_vec = np.array(qci.get_coeff_vec()) # Reset stae qci.set_coeff_vec(ci) qci.apply_sparse_matrix(sp_mat_op) # see if there is a difference diff_vec = np.array(qci.get_coeff_vec()) - diff_vec diff_val = np.linalg.norm(diff_vec) print('Operator used for sparse matrix operator test: \n', qubit_op) print('||∆||: ', diff_val) assert diff_val < 1.0e-15
def test_H2_qite(self): # The FCI energy for H2 at 1.5 Angstrom in a sto-3g basis E_fci = -0.9981493534 coef_vec = [ -0.4917857774144603, 0.09345649662931771, 0.09345649662931771, -0.0356448161226769, -0.0356448161226769, 0.1381758457453024, 0.05738398402634884, -0.0573839840263488, -0.0573839840263488, 0.05738398402634884, 0.08253705485911705, 0.13992103888546592, 0.13992103888546592, 0.08253705485911705, 0.1458551902800438 ] circ_vec = [ Circuit(), build_circuit('Z_0'), build_circuit('Z_1'), build_circuit('Z_2'), build_circuit('Z_3'), build_circuit('Z_0 Z_1'), build_circuit('Y_0 X_1 X_2 Y_3'), build_circuit('X_0 X_1 Y_2 Y_3'), build_circuit('Y_0 Y_1 X_2 X_3'), build_circuit('X_0 Y_1 Y_2 X_3'), build_circuit('Z_0 Z_2'), build_circuit('Z_0 Z_3'), build_circuit('Z_1 Z_2'), build_circuit('Z_1 Z_3'), build_circuit('Z_2 Z_3') ] H2_qubit_hamiltonian = QubitOperator() for i in range(len(circ_vec)): H2_qubit_hamiltonian.add(coef_vec[i], circ_vec[i]) H2_sq_hamiltonian = [[(), 0.3527848071133334], [(0, 0), -0.9081808722384057], [(1, 1), -0.9081808722384057], [(2, 2), -0.6653369358038996], [(3, 3), -0.6653369358038996], [(1, 0, 1, 0), -0.5527033829812091], [(1, 0, 3, 2), -0.22953593610539536], [(2, 0, 2, 0), -0.3301482194364681], [(3, 0, 2, 1), 0.22953593610539527], [(3, 0, 3, 0), -0.5596841555418633], [(2, 1, 3, 0), 0.22953593610539527], [(2, 1, 2, 1), -0.5596841555418633], [(3, 1, 3, 1), -0.3301482194364681], [(3, 2, 1, 0), -0.22953593610539522], [(3, 2, 3, 2), -0.5834207611201749]] ref = [1, 1, 0, 0] print('\nBegin QITE test for H2') print('-------------------------') # make test with algorithm class # mol = Molecule() mol.hamiltonian = H2_qubit_hamiltonian mol.sq_hamiltonian = H2_sq_hamiltonian alg = QITE(mol, reference=ref) alg.run(beta=18.0, do_lanczos=True, lanczos_gap=49) Egs = alg.get_gs_energy() assert Egs == approx(E_fci, abs=1.0e-10)
def test_jw1(self): """ test JW transform of 1.0(2^ 3^ 4 6) + 1.5(1^ 2^ 3 4) """ coeff_vec = [ -0.093750, +0.093750j, -0.093750, -0.093750j, -0.093750, -0.093750j, +0.062500j, -0.093750, -0.093750, +0.093750j, +0.093750, -0.062500, -0.093750j, -0.062500j, +0.062500j, -0.062500, +0.062500, +0.062500, -0.062500j, -0.093750, +0.062500j, -0.062500, -0.062500j, -0.062500, +0.093750j, +0.093750j, +0.062500j, +0.093750, -0.062500, -0.062500, -0.093750j, -0.062500j ] circ_vec = [ build_circuit('X_1 Y_2 X_3 Y_4'), build_circuit('X_1 Y_2 X_3 X_4'), build_circuit('X_1 Y_2 Y_3 X_4'), build_circuit('X_1 X_2 X_3 Y_4'), build_circuit('X_1 X_2 X_3 X_4'), build_circuit('X_1 X_2 Y_3 X_4'), build_circuit('Y_2 X_3 X_4 Z_5 X_6'), build_circuit('Y_1 Y_2 Y_3 Y_4'), build_circuit('Y_1 X_2 X_3 Y_4'), build_circuit('Y_1 X_2 X_3 X_4'), build_circuit('Y_1 Y_2 X_3 X_4'), build_circuit('X_2 X_3 X_4 Z_5 X_6'), build_circuit('Y_1 X_2 Y_3 Y_4'), build_circuit('X_2 Y_3 Y_4 Z_5 Y_6'), build_circuit('X_2 Y_3 X_4 Z_5 X_6'), build_circuit('X_2 Y_3 Y_4 Z_5 X_6'), build_circuit('X_2 X_3 Y_4 Z_5 Y_6'), build_circuit('Y_2 Y_3 X_4 Z_5 X_6'), build_circuit('X_2 X_3 Y_4 Z_5 X_6'), build_circuit('Y_1 X_2 Y_3 X_4'), build_circuit('Y_2 Y_3 X_4 Z_5 Y_6'), build_circuit('Y_2 X_3 X_4 Z_5 Y_6'), build_circuit('X_2 X_3 X_4 Z_5 Y_6'), build_circuit('X_2 Y_3 X_4 Z_5 Y_6'), build_circuit('Y_1 Y_2 Y_3 X_4'), build_circuit('Y_1 Y_2 X_3 Y_4'), build_circuit('Y_2 Y_3 Y_4 Z_5 X_6'), build_circuit('X_1 X_2 Y_3 Y_4'), build_circuit('Y_2 Y_3 Y_4 Z_5 Y_6'), build_circuit('Y_2 X_3 Y_4 Z_5 X_6'), build_circuit('X_1 Y_2 Y_3 Y_4'), build_circuit('Y_2 X_3 Y_4 Z_5 Y_6') ] correct_op = QubitOperator() for coeff, circ in zip(coeff_vec, circ_vec): correct_op.add(coeff, circ) # Test qforte construction a1 = SQOperator() a1.add(1.00, [2, 3], [4, 6]) a1.add(+1.50, [1, 2], [3, 4]) print(a1.str(), '\n') aop = a1.jw_transform() print(aop.str(), '\n') assert aop.check_op_equivalence(correct_op, True) is True
def test_circ_op_print(self): # initialize empty circuit circ1 = Circuit() # add (Z1 H2 Y4 X4) Pauli string # note: the rightmost gate is applied first circ1.add(gate('X', 4)) circ1.add(gate('Y', 4)) circ1.add(gate('H', 2)) circ1.add(gate('Z', 1)) # test built-in print function out = str(circ1) assert out == '[Z1 H2 Y4 X4]' # test smart_print function with patch('sys.stdout', new=StringIO()) as fake_out: smart_print(circ1) assert fake_out.getvalue( ) == '\n Quantum circuit:\n(Z1 H2 Y4 X4) |Ψ>\n' # initialize empty circuit circ2 = Circuit() # add (H1 Y2 S3 I4) Pauli string # note: the rightmost gate is applied first circ2.add(gate('I', 4)) circ2.add(gate('S', 3)) circ2.add(gate('Y', 2)) circ2.add(gate('H', 1)) # initialize empty circuit circ3 = Circuit() # add (X1 Y2 H3 Z4) Pauli string # note: the rightmost gate is applied first circ3.add(gate('Z', 4)) circ3.add(gate('H', 3)) circ3.add(gate('Y', 2)) circ3.add(gate('X', 1)) # initialize empty QubitOperator q_op = QubitOperator() # add u1*circ1 + u2*circ2 + u3*circ3 u1 = 0.5 + 0.1j u2 = -0.5j u3 = +0.3 q_op.add(u1, circ1) q_op.add(u2, circ2) q_op.add(u3, circ3) # test built-in print function out = str(q_op) assert out == '+0.500000 +0.100000i[Z1 H2 Y4 X4]\n'\ '-0.500000j[H1 Y2 S3 I4]\n'\ '+0.300000[X1 Y2 H3 Z4]' # test smart_print function with patch('sys.stdout', new=StringIO()) as fake_out: smart_print(q_op) assert fake_out.getvalue() == '\n Quantum operator:\n(0.5+0.1j) (Z1 H2 Y4 X4) |Ψ>\n'\ '+ (-0-0.5j) (H1 Y2 S3 I4) |Ψ>\n'\ '+ (0.3+0j) (X1 Y2 H3 Z4) |Ψ>\n'