Commit 2b1811ab authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Added timing loops for test molecules



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent da5de09b
Loading
Loading
Loading
Loading
+31 −24
Original line number Diff line number Diff line
@@ -2,18 +2,26 @@ from qcor import *
import math, time
import time

# Create the H20 hamiltonian with Pyscf
molecule_string = 'O 0 0 0; H 0 -2.757 2.587; H 0 2.757  2.587'
H = createOperator('pyscf', {'basis': 'sto-3g', 'geometry': molecule_string})

nQubits = H.nBits()
nElectrons = 10
# Molecules to test
h2o_mole = 'O 0 0 0; H 0 -2.757 2.587; H 0 2.757  2.587'
n2_mol = 'N 0.0, 0.0, 0.56499; N 0.0, 0.0, -0.56499'
hcn_mole = 'C 0.0, 0.0, -0.511747; N 0.0, 0.0, 0.664461; H 0.0, 0.0, -1.580746'
test_cases = { "H20": h2o_mole, "N2": n2_mol, "HCN": hcn_mole }
# Data for validation
nElectrons_data = { "H20": 10, "N2": 14, "HCN": 14 }
nQubits_data = { "H20": 14, "N2": 20, "HCN": 22 }

# print(H.toString())
@qjit
def ansatz(q : qreg, n_electron: int, params : List[float]):
    uccsd(q, q.size(), n_electron, params)

for test_case in test_cases:
    print("Run ", test_case)
    molecule_string = test_cases[test_case]
    H = createOperator('pyscf', {'basis': 'sto-3g', 'geometry': molecule_string})
    nQubits = H.nBits()
    assert nQubits == nQubits_data[test_case]
    nElectrons = nElectrons_data[test_case]
    q = qalloc(nQubits)
    nOccupied = math.ceil(nElectrons / 2.0)
    nVirtual = nQubits / 2 - nOccupied
@@ -25,7 +33,6 @@ print("Number of parameters =", nParameters)
    params = []
    for i in range(nParameters):
        params.append(1.0)

    start = time.time()
    ansatz.print_kernel(q, nElectrons, params)
    end = time.time()
+44 −34
Original line number Diff line number Diff line
@@ -9,37 +9,47 @@ from qiskit.chemistry.components.initial_states import HartreeFock
from qiskit.chemistry.components.variational_forms import UCCSD
from qiskit.chemistry.drivers import PySCFDriver, UnitsType
from qiskit.chemistry.applications import MolecularGroundStateEnergy

import time

basis_string = 'sto-3g'
# Molecules to test
h2o_mole = 'O 0 0 0; H 0 -2.757 2.587; H 0 2.757  2.587'
n2_mol = '0.0, 0.0, 0.56499; N 0.0, 0.0, -0.56499'
n2_mol = 'N 0.0, 0.0, 0.56499; N 0.0, 0.0, -0.56499'
hcn_mole = 'C 0.0, 0.0, -0.511747; N 0.0, 0.0, 0.664461; H 0.0, 0.0, -1.580746'
driver = PySCFDriver(atom=h2o_mole, basis = 'sto-3g')
test_cases = { "H20": h2o_mole, "N2": n2_mol, "HCN": hcn_mole }

for test_case in test_cases:
  print("Run ", test_case)
  mole_str = test_cases[test_case]
  driver = PySCFDriver(atom=mole_str, basis = 'sto-3g')
  def cb_create_solver(num_particles, num_orbitals,
                          qubit_mapping, two_qubit_reduction, z2_symmetries):
      print('num_particles =', num_particles, "; num_orbitals =", num_orbitals)
      initial_state = HartreeFock(num_orbitals, num_particles, qubit_mapping,
                                  two_qubit_reduction, z2_symmetries.sq_list)
      start = time.time()
      var_form = UCCSD(num_orbitals=num_orbitals,
                          num_particles=num_particles,
                          initial_state=initial_state,
                          qubit_mapping=qubit_mapping,
                          two_qubit_reduction=two_qubit_reduction,
                          z2_symmetries=z2_symmetries)
      end = time.time()
      print("UCCSD var-form ctor time:", end - start, " [secs]")

      # Print circuit at each iteration.
      def iter_cb_func(eval_count, parameters, mean, std):
        print("Run ", eval_count, ": E = ", mean)
        start = time.time()
        iter_circ = var_form.construct_circuit(parameters).decompose()
        end = time.time()
        print("Circuit eval time:", end - start, " [secs]")
        #print(iter_circ.qasm())
        op_count = iter_circ.count_ops()
        for op_name in op_count:
          print(op_name, ":", op_count[op_name])
      
    vqe = VQE(var_form=var_form, optimizer=SLSQP(maxiter=500), include_custom=True, callback=iter_cb_func)
      vqe = VQE(var_form=var_form, optimizer=SLSQP(maxiter=1), include_custom=True, callback=iter_cb_func)
      vqe.quantum_instance = Aer.get_backend('qasm_simulator')
      return vqe