def circuit_tester(prep, test_circ): for gate in test_circ.gates(): id = gate.gate_id() target = gate.target() control = gate.control() num_qubits = 5 qc1 = qforte.QuantumComputer(num_qubits) qc2 = qforte.QuantumComputer(num_qubits) qc1.apply_circuit_safe(prep) qc2.apply_circuit_safe(prep) qc1.apply_gate_safe(gate) qc2.apply_gate(gate) C1 = qc1.get_coeff_vec() C2 = qc2.get_coeff_vec() diff_vec = [(C1[i] - C2[i]) * np.conj(C1[i] - C2[i]) for i in range(len(C1))] diff_norm = np.sum(diff_vec) if (np.sum(diff_vec) != (0.0 + 0.0j)): print('|C - C_safe|F^2 control target id') print('----------------------------------------') print(diff_norm, ' ', control, ' ', target, ' ', id) return diff_norm
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_trotterization(self): circ_vec = [qforte.QuantumCircuit(), qforte.build_circuit('Z_0')] coef_vec = [-1.0j * 0.5, -1.0j * -0.04544288414432624] # 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]) # exponentiate the operator Utrot, phase = qforte.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 = qforte.QuantumComputer(4) qc.set_coeff_vec(inital_state) # apply the troterized minus_iH qc.apply_circuit(Utrot) qc.apply_constant(phase) qforte.smart_print(qc) coeffs = qc.get_coeff_vec() self.assertAlmostEqual(np.real(coeffs[3]), 0.6980209737879599) self.assertAlmostEqual(np.imag(coeffs[3]), -0.423595782342996) self.assertAlmostEqual(np.real(coeffs[12]), -0.5187235657531178) self.assertAlmostEqual(np.imag(coeffs[12]), 0.25349397560041553)
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_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_io_simplified(self): # test direct expectation value measurement trial_state = qforte.QuantumComputer(4) trial_circ = qforte.build_circuit('H_0 H_1 H_2 H_3 cX_0_1') # use circuit to prepare trial state trial_state.apply_circuit(trial_circ) #build the quantum operator for [a1^ a2] a1_dag_a2 = qforte.build_operator('0.0-0.25j, X_2 Y_1; 0.25, Y_2 Y_1; \ 0.25, X_2 X_1; 0.0+0.25j, Y_2 X_1') #get direct expectatoin value exp = trial_state.direct_op_exp_val(a1_dag_a2) self.assertAlmostEqual(exp, 0.2499999999999999 + 0.0j)
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_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_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))