def test_trotterization_with_controlled_U(self): circ_vec = [ qforte.build_circuit('Y_0 X_1'), qforte.build_circuit('X_0 Y_1') ] coef_vec = [-1.0719145972781818j, 1.0719145972781818j] # the operator to be exponentiated minus_iH = qforte.QuantumOperator() for i in range(len(circ_vec)): minus_iH.add_term(coef_vec[i], circ_vec[i]) ancilla_idx = 2 # exponentiate the operator Utrot, phase = qforte.trotterization.trotterize_w_cRz( minus_iH, ancilla_idx) # Case 1: positive control # initalize a quantum computer qc = qforte.QuantumComputer(3) # build HF state qc.apply_gate(qforte.gate('X', 0, 0)) # put ancilla in |1> state qc.apply_gate(qforte.gate('X', 2, 2)) # apply the troterized minus_iH qc.apply_circuit(Utrot) qforte.smart_print(qc) coeffs = qc.get_coeff_vec() self.assertAlmostEqual(coeffs[5], -0.5421829373 + 0.0j) self.assertAlmostEqual(coeffs[6], -0.840260473 + 0.0j) # Case 2: negitive control # initalize a quantum computer qc = qforte.QuantumComputer(3) # build HF state qc.apply_gate(qforte.gate('X', 0, 0)) # apply the troterized minus_iH qc.apply_circuit(Utrot) qforte.smart_print(qc) coeffs = qc.get_coeff_vec() self.assertAlmostEqual(coeffs[1], 1.0 + 0.0j)
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 = [ qforte.QuantumCircuit(), qforte.build_circuit('Z_0'), qforte.build_circuit('Z_1'), qforte.build_circuit('Z_2'), qforte.build_circuit('Z_3'), qforte.build_circuit('Z_0 Z_1'), qforte.build_circuit('Y_0 X_1 X_2 Y_3'), qforte.build_circuit('Y_0 Y_1 X_2 X_3'), qforte.build_circuit('X_0 X_1 Y_2 Y_3'), qforte.build_circuit('X_0 Y_1 Y_2 X_3'), qforte.build_circuit('Z_0 Z_2'), qforte.build_circuit('Z_0 Z_3'), qforte.build_circuit('Z_1 Z_2'), qforte.build_circuit('Z_1 Z_3'), qforte.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 = qforte.QuantumOperator() for i in range(len(circ_vec)): H2_qubit_hamiltonian.add_term(coef_vec[i], circ_vec[i]) # circuit for making HF state circ = qforte.QuantumCircuit() circ.add_gate(qforte.gate('X', 0, 0)) circ.add_gate(qforte.gate('X', 1, 1)) TestExperiment = qforte.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) self.assertAlmostEqual(experimental_error, 0.0)
def generic_test_circ_vec_builder(qb_list, id): circ_vec_tc = [qforte.QuantumCircuit() for i in range(len(qb_list))] circ_vec_ct = [qforte.QuantumCircuit() for i in range(len(qb_list))] for i, pair in enumerate(ct_lst): t = pair[0] c = pair[1] if (id == 'cR'): circ_vec_ct[i].add_gate(qforte.gate(id, t, c, 3.17 * t * c)) circ_vec_tc[i].add_gate(qforte.gate(id, c, t, 1.41 * t * c)) else: circ_vec_ct[i].add_gate(qforte.gate(id, t, c)) circ_vec_tc[i].add_gate(qforte.gate(id, c, t)) return circ_vec_tc, circ_vec_ct
def test_op_exp_val_1(self): # test direct expectation value measurement trial_state = qforte.QuantumComputer(4) trial_prep = [None] * 5 trial_prep[0] = qforte.gate('H', 0, 0) trial_prep[1] = qforte.gate('H', 1, 1) trial_prep[2] = qforte.gate('H', 2, 2) trial_prep[3] = qforte.gate('H', 3, 3) trial_prep[4] = qforte.gate('cX', 0, 1) trial_circ = qforte.QuantumCircuit() #prepare the circuit for gate in trial_prep: trial_circ.add_gate(gate) # use circuit to prepare trial state trial_state.apply_circuit(trial_circ) # gates needed for [a1^ a2] operator X1 = qforte.gate('X', 1, 1) X2 = qforte.gate('X', 2, 2) Y1 = qforte.gate('Y', 1, 1) Y2 = qforte.gate('Y', 2, 2) # initialize circuits to make operator circ1 = qforte.QuantumCircuit() circ1.add_gate(X2) circ1.add_gate(Y1) circ2 = qforte.QuantumCircuit() circ2.add_gate(Y2) circ2.add_gate(Y1) circ3 = qforte.QuantumCircuit() circ3.add_gate(X2) circ3.add_gate(X1) circ4 = qforte.QuantumCircuit() circ4.add_gate(Y2) circ4.add_gate(X1) #build the quantum operator for [a1^ a2] a1_dag_a2 = qforte.QuantumOperator() a1_dag_a2.add_term(0.0 - 0.25j, circ1) a1_dag_a2.add_term(0.25, circ2) a1_dag_a2.add_term(0.25, circ3) a1_dag_a2.add_term(0.0 + 0.25j, circ4) #get direct expectatoin value exp = trial_state.direct_op_exp_val(a1_dag_a2) self.assertAlmostEqual(exp, 0.2499999999999999 + 0.0j)
def test_cY_gate(self): # test the cY gate nqubits = 2 basis0 = make_basis('00') # basis0:|00> basis1 = make_basis('01') # basis1:|10> basis2 = make_basis('10') # basis2:|01> basis3 = make_basis('11') # basis3:|11> computer = qforte.QuantumComputer(nqubits) cY = qforte.gate('cY', 0, 1) # test cY|00> = |00> computer.set_state([(basis0, 1.0)]) computer.apply_gate(cY) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) coeff2 = computer.coeff(basis2) coeff3 = computer.coeff(basis3) self.assertAlmostEqual(coeff0, 1.0 + 0.0j) self.assertAlmostEqual(coeff1, 0.0 + 0.0j) self.assertAlmostEqual(coeff2, 0.0 + 0.0j) self.assertAlmostEqual(coeff3, 0.0 + 0.0j) # test cY|01> = |01> computer.set_state([(basis2, 1.0)]) computer.apply_gate(cY) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) coeff2 = computer.coeff(basis2) coeff3 = computer.coeff(basis3) self.assertAlmostEqual(coeff0, 0.0 + 0.0j) self.assertAlmostEqual(coeff1, 0.0 + 0.0j) self.assertAlmostEqual(coeff2, 1.0 + 0.0j) self.assertAlmostEqual(coeff3, 0.0 + 0.0j) # test cY|10> = i|11> computer.set_state([(basis1, 1.0)]) computer.apply_gate(cY) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) coeff2 = computer.coeff(basis2) coeff3 = computer.coeff(basis3) self.assertAlmostEqual(coeff0, 0.0 + 0.0j) self.assertAlmostEqual(coeff1, 0.0 + 0.0j) self.assertAlmostEqual(coeff2, 0.0 + 0.0j) self.assertAlmostEqual(coeff3, 0.0 + 1.0j) # test cY|11> = -i|10> computer.set_state([(basis3, 1.0)]) computer.apply_gate(cY) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) coeff2 = computer.coeff(basis2) coeff3 = computer.coeff(basis3) self.assertAlmostEqual(coeff0, 0.0 + 0.0j) self.assertAlmostEqual(coeff1, 0.0 - 1.0j) self.assertAlmostEqual(coeff2, 0.0 + 0.0j) self.assertAlmostEqual(coeff3, 0.0 + 0.0j)
def test_computer(self): print('\n') # test that 1 - 1 = 0 # print('\n'.join(qc.str())) X = qforte.gate('X', 0, 0) print(X) Y = qforte.gate('Y', 0, 0) print(Y) Z = qforte.gate('Z', 0, 0) print(Z) H = qforte.gate('H', 0, 0) print(H) R = qforte.gate('R', 0, 0, 0.1) print(R) S = qforte.gate('S', 0, 0) print(S) T = qforte.gate('T', 0, 0) print(T) cX = qforte.gate('cX', 0, 1) print(cX) cY = qforte.gate('cY', 0, 1) print(cY) cZ = qforte.gate('cZ', 0, 1) print(cZ) # qcircuit = qforte.QuantumCircuit() # qcircuit.add_gate(qg) # qcircuit.add_gate(qforte.QuantumGate(qforte.QuantumGateType.Hgate,1,1)); # print('\n'.join(qcircuit.str())) # self.assertEqual(qforte.subtract(1, 1), 0) computer = qforte.QuantumComputer(16) # print(repr(computer)) # circuit = qforte.QuantumCircuit() # circuit.add_gate(X) for i in range(3000): computer.apply_gate(X) computer.apply_gate(Y) computer.apply_gate(Z) computer.apply_gate(H)
def test_Z_gate(self): # test the Pauli Y gate nqubits = 1 basis0 = make_basis('0') basis1 = make_basis('1') computer = qforte.QuantumComputer(nqubits) Z = qforte.gate('Z', 0, 0) # test Z|0> = |0> computer.apply_gate(Z) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) self.assertAlmostEqual(coeff0, 1.0 + 0.0j) self.assertAlmostEqual(coeff1, 0.0 + 0.0j) # test Z|1> = -|1> computer.set_state([(basis1, 1.0)]) computer.apply_gate(Z) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) self.assertAlmostEqual(coeff0, 0.0 + 0.0j) self.assertAlmostEqual(coeff1, -1.0 + 0.0j)
def test_Y_gate(self): # test the Pauli Y gate nqubits = 1 basis0 = make_basis('0') basis1 = make_basis('1') computer = qforte.QuantumComputer(nqubits) Y = qforte.gate('Y', 0, 0) # test Y|0> = i|1> computer.apply_gate(Y) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) self.assertAlmostEqual(coeff0, 0.0 + 0.0j) self.assertAlmostEqual(coeff1, 0.0 + 1.0j) # test Y|1> = -i|0> computer.set_state([(basis1, 1.0)]) computer.apply_gate(Y) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) self.assertAlmostEqual(coeff0, 0.0 - 1.0j) self.assertAlmostEqual(coeff1, 0.0 + 0.0j)
def test_cX_gate(self): # test the cX/CNOT gate nqubits = 2 basis0 = make_basis('00') # basis0:|00> basis1 = make_basis('01') # basis1:|10> basis2 = make_basis('10') # basis2:|01> basis3 = make_basis('11') # basis3:|11> computer = qforte.QuantumComputer(nqubits) CNOT = qforte.gate('CNOT', 0, 1) # test CNOT|00> = |00> computer.set_state([(basis0, 1.0)]) computer.apply_gate(CNOT) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) coeff2 = computer.coeff(basis2) coeff3 = computer.coeff(basis3) self.assertAlmostEqual(coeff0, 1.0 + 0.0j) self.assertAlmostEqual(coeff1, 0.0 + 0.0j) self.assertAlmostEqual(coeff2, 0.0 + 0.0j) self.assertAlmostEqual(coeff3, 0.0 + 0.0j) # test CNOT|10> = |11> computer.set_state([(basis1, 1.0)]) computer.apply_gate(CNOT) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) coeff2 = computer.coeff(basis2) coeff3 = computer.coeff(basis3) self.assertAlmostEqual(coeff0, 0.0 + 0.0j) self.assertAlmostEqual(coeff1, 0.0 + 0.0j) self.assertAlmostEqual(coeff2, 0.0 + 0.0j) self.assertAlmostEqual(coeff3, 1.0 + 0.0j) # test CNOT|01> = |01> computer.set_state([(basis2, 1.0)]) computer.apply_gate(CNOT) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) coeff2 = computer.coeff(basis2) coeff3 = computer.coeff(basis3) self.assertAlmostEqual(coeff0, 0.0 + 0.0j) self.assertAlmostEqual(coeff1, 0.0 + 0.0j) self.assertAlmostEqual(coeff2, 1.0 + 0.0j) self.assertAlmostEqual(coeff3, 0.0 + 0.0j) # test CNOT|11> = |10> computer.set_state([(basis3, 1.0)]) computer.apply_gate(CNOT) coeff0 = computer.coeff(basis0) coeff1 = computer.coeff(basis1) coeff2 = computer.coeff(basis2) coeff3 = computer.coeff(basis3) self.assertAlmostEqual(coeff0, 0.0 + 0.0j) self.assertAlmostEqual(coeff1, 1.0 + 0.0j) self.assertAlmostEqual(coeff2, 0.0 + 0.0j) self.assertAlmostEqual(coeff3, 0.0 + 0.0j) with self.assertRaises(ValueError) as context: qforte.gate('CNOT', 0, 1.0) self.assertTrue(')' in str(context.exception))
import unittest # import our `pybind11`-based extension module from package qforte from qforte import qforte import numpy as np num_qubits = 5 prep_circ = qforte.QuantumCircuit() ct_lst = [(4, 3), (4, 2), (4, 1), (4, 0), (3, 2), (3, 1), (3, 0), (2, 1), (2, 0), (1, 0)] for i in range(num_qubits): prep_circ.add_gate(qforte.gate('H', i, i)) for i in range(num_qubits): prep_circ.add_gate(qforte.gate('cR', i, i + 1, 1.116 / (i + 1.0))) def generic_test_circ_vec_builder(qb_list, id): circ_vec_tc = [qforte.QuantumCircuit() for i in range(len(qb_list))] circ_vec_ct = [qforte.QuantumCircuit() for i in range(len(qb_list))] for i, pair in enumerate(ct_lst): t = pair[0] c = pair[1] if (id == 'cR'): circ_vec_ct[i].add_gate(qforte.gate(id, t, c, 3.17 * t * c)) circ_vec_tc[i].add_gate(qforte.gate(id, c, t, 1.41 * t * c)) else: circ_vec_ct[i].add_gate(qforte.gate(id, t, c)) circ_vec_tc[i].add_gate(qforte.gate(id, c, t))