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

Simple hook to use XACC UCCSD circuit generator for benchmarking



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent f4146f83
Loading
Loading
Loading
Loading
+24 −5
Original line number Diff line number Diff line
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

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

q = qalloc(nQubits)
nOccupied = math.ceil(nElectrons / 2.0)
nVirtual = nQubits / 2 - nOccupied
nOrbitals = nOccupied + nVirtual
nSingle = nOccupied * nVirtual
nDouble = nSingle * (nSingle + 1) / 2
nParameters = int(nSingle + nDouble)
print("Number of parameters =", nParameters)
params = []
for i in range(nParameters):
    params.append(1.0)

q = qalloc(4)
ansatz.print_kernel(q, [])
 No newline at end of file
start = time.time()
ansatz.print_kernel(q, nElectrons, params)
end = time.time()
print("Kernel eval time:", end - start, " [secs]")
+2 −2
Original line number Diff line number Diff line
@@ -13,10 +13,10 @@ from qiskit.chemistry.applications import MolecularGroundStateEnergy

basis_string = 'sto-3g'
# Molecules to test
h20_mole = 'O 0 0 0; H 0 -2.757 2.587; H 0 2.757  2.587'
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'
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=hcn_mole, basis = 'sto-3g')
driver = PySCFDriver(atom=h2o_mole, basis = 'sto-3g')

def cb_create_solver(num_particles, num_orbitals,
                        qubit_mapping, two_qubit_reduction, z2_symmetries):
+15 −0
Original line number Diff line number Diff line
@@ -149,6 +149,21 @@ class pyxasm_visitor : public pyxasmBaseVisitor {
               << ");\n";
            result.first = ss.str();
          }
          else if (inst_name == "uccsd") {
            // std::cout << "Get UCCSD: " << context->getText() << "\n";
            std::stringstream ss;
            // Delegate to the QRT call directly.
            ss << "quantum::uccsd("
               << context->trailer()[0]->arglist()->argument(0)->getText()
               << ", "
               << context->trailer()[0]->arglist()->argument(1)->getText()
               << ", "
               << context->trailer()[0]->arglist()->argument(2)->getText()
               << ", "
               << context->trailer()[0]->arglist()->argument(3)->getText()
               << ");\n";
            result.first = ss.str();
          }
          // Handle potential name collision: user-defined kernel having the
          // same name as an XACC circuit: e.g. common names such as qft, iqft
          // Note: these circuits (except exp_i_theta) don't have QRT
+12 −0
Original line number Diff line number Diff line
@@ -201,6 +201,18 @@ void exp(qreg q, const double theta, std::shared_ptr<xacc::Observable> H) {
  qrt_impl->exp(q, theta, H);
}

void uccsd(qreg q, int nq, int ne, const std::vector<double> &thetas) {
  auto tmp = xacc::getService<xacc::Instruction>("uccsd");
  auto uccsd = std::dynamic_pointer_cast<xacc::CompositeInstruction>(tmp);
  uccsd->expand({{"ne", ne}, {"nq", nq}});
  // std::cout << "HOWDY:\n" << uccsd->toString() << "\n";
  auto evaled = uccsd->operator()(thetas);
  // std::cout << "HOWDY:\n" << evaled->toString() << "\n";
  for (auto inst : evaled->getInstructions()) {
    qrt_impl->get_current_program()->addInstruction(inst);
  }
}

void submit(xacc::AcceleratorBuffer *buffer) { qrt_impl->submit(buffer); }

void submit(xacc::AcceleratorBuffer **buffers, const int nBuffers) {
+1 −0
Original line number Diff line number Diff line
@@ -144,6 +144,7 @@ void crz(const qubit &src_idx, const qubit &tgt_idx, const double theta);
void exp(qreg q, const double theta, xacc::Observable &H);
void exp(qreg q, const double theta, xacc::Observable *H);
void exp(qreg q, const double theta, std::shared_ptr<xacc::Observable> H);
void uccsd(qreg q, int nq, int ne, const std::vector<double> &thetas);

// Submission API. Submit the constructed CompositeInstruction operating
// on the provided AcceleratorBuffer(s) (note qreg wraps an AcceleratorBuffer)