Exemplo n.º 1
0
    def test_vqe(self):
        """Test VQE with gradients"""
        method = 'lin_comb'
        backend = 'qasm_simulator'
        q_instance = QuantumInstance(BasicAer.get_backend(backend), seed_simulator=79,
                                     seed_transpiler=2)
        # Define the Hamiltonian
        h2_hamiltonian = -1.05 * (I ^ I) + 0.39 * (I ^ Z) - 0.39 * (Z ^ I) \
                         - 0.01 * (Z ^ Z) + 0.18 * (X ^ X)
        h2_energy = -1.85727503

        # Define the Ansatz
        wavefunction = QuantumCircuit(2)
        params = ParameterVector('theta', length=8)
        itr = iter(params)
        wavefunction.ry(next(itr), 0)
        wavefunction.ry(next(itr), 1)
        wavefunction.rz(next(itr), 0)
        wavefunction.rz(next(itr), 1)
        wavefunction.cx(0, 1)
        wavefunction.ry(next(itr), 0)
        wavefunction.ry(next(itr), 1)
        wavefunction.rz(next(itr), 0)
        wavefunction.rz(next(itr), 1)

        # Conjugate Gradient algorithm
        optimizer = CG(maxiter=10)

        grad = Gradient(grad_method=method)

        # Gradient callable
        vqe = VQE(h2_hamiltonian, wavefunction, optimizer=optimizer, gradient=grad)

        result = vqe.run(q_instance)
        np.testing.assert_almost_equal(result['optimal_value'], h2_energy, decimal=0)
Exemplo n.º 2
0
 def test_qgan_training_cg(self):
     """Test QGAN training."""
     optimizer = CG(maxiter=1)
     self.qgan.set_generator(generator_circuit=self.generator_circuit,
                             generator_optimizer=optimizer)
     trained_statevector = self.qgan.run(self.qi_statevector)
     trained_qasm = self.qgan.run(self.qi_qasm)
     self.assertAlmostEqual(trained_qasm['rel_entr'], trained_statevector['rel_entr'], delta=0.1)
Exemplo n.º 3
0
 def test_cg(self):
     """ cg test """
     optimizer = CG(maxiter=1000, tol=1e-06)
     res = self._optimize(optimizer)
     self.assertLessEqual(res[2], 10000)
Exemplo n.º 4
0
                         qubit_mapping=core._qubit_mapping,
                         two_qubit_reduction=core._two_qubit_reduction,
                         num_particles=core._molecule_info['num_particles'],
                         sq_list=sqlist)
circuit = init_state.construct_circuit()

outfile.write("\nHartree-Fock energy %f \n" % (molecule.hf_energy))
outfile.write("\nHartree-Fock circuit\n")
outfile.write(str(circuit.draw()) + "\n")

var_form = RY(H_op.num_qubits,
              depth=3,
              entanglement='full',
              entanglement_gate='cz',
              initial_state=init_state)
optimizer = CG(maxiter=1000)
algo = VQE(H_op, var_form, optimizer, aux_operators=A_op)

backend = Aer.get_backend('statevector_simulator')
quantum_instance = QuantumInstance(backend=backend)
algo_result = algo.run(quantum_instance)
get_results(H_op, A_op, molecule, core, algo_result, outfile)

outfile.write("\nRY circuit\n")
outfile.write(
    str(var_form.construct_circuit(algo_result['optimal_point']).draw()) +
    "\n")

print_circuit_requirements(
    var_form.construct_circuit(algo_result['optimal_point']),
    'ibmq_16_melbourne', 3, range(H_op.num_qubits), outfile)
var_form = UCCSD(reps=1,
                 num_orbitals=core._molecule_info['num_orbitals'],
                 num_particles=core._molecule_info['num_particles'],
                 active_occupied=None,
                 active_unoccupied=None,
                 initial_state=init_state,
                 qubit_mapping=core._qubit_mapping,
                 two_qubit_reduction=core._two_qubit_reduction,
                 num_time_slices=1)
init_parm = np.zeros(var_form.num_parameters)

# 4) Initialize VQE with statevector simulator and run

print("Running  VQE on statevector simulator ...")
optimizer = CG(
    maxiter=100
)  # We chose the CG optimizer but you can choose COBYLA, SPSA, L_BFGS_B, ...
algo = VQE(qubit_op,
           var_form,
           optimizer,
           aux_operators=aux_ops,
           initial_point=init_parm)
simulator = Aer.get_backend("qasm_simulator")
backend_options = {"method": "statevector"}
quantum_instance = QuantumInstance(backend=simulator,
                                   backend_options=backend_options)
algo_result = algo.run(quantum_instance)

# 5) Print the results

print("\nResults")
Exemplo n.º 6
0
def run_ucc_r12(opt):
    """
    Parameters
    ----------
    * opt dictionary with user and default options/thresholds
    Returns
    -------
    * UCC-R12 ground state energy
        
    """

    import itertools
    import logging
    import numpy as np
    import time
    from datetime import datetime

    import qiskit    
    from qiskit                            import BasicAer,Aer    
    from qiskit.aqua                       import set_qiskit_aqua_logging, QuantumInstance
    from qiskit.aqua.operators             import Z2Symmetries, WeightedPauliOperator
    from qiskit.aqua.algorithms.adaptive   import VQE
    from qiskit.aqua.algorithms.classical  import ExactEigensolver
    from qiskit.aqua.components.optimizers import L_BFGS_B,CG,SPSA,SLSQP, COBYLA

    from qiskit.chemistry.drivers                                      import PySCFDriver, UnitsType, HFMethodType
    from qiskit.chemistry.core                                         import TransformationType, QubitMappingType
    from qiskit.chemistry.aqua_extensions.components.initial_states    import HartreeFock
    from qiskit.chemistry                                              import set_qiskit_chemistry_logging

    # ----- local functions ----- #
    import sys
    sys.path.append('/Users/mario/Documents/GitHub/R12-F12/project_ec/ucc_ec/')
    from qmolecule_ec   import QMolecule_ec,build_parameter_list
    from hamiltonian_ec import Hamiltonian
    from uccsd_ec       import UCCSD

    from davidson import davidson

    #import logging
    #set_qiskit_chemistry_logging(logging.DEBUG)

    logfile = open(opt["logfile"],'w')
    dateTimeObj = datetime.now()
    logfile.write("date and time "+str(dateTimeObj)+"\n")
    logfile.write("\n\n")

    import subprocess
    label = subprocess.check_output(['git','log','-1','--format="%H"']).strip()
    logfile.write("git commit "+str(label)+"\n")
    logfile.write("\n\n")

    qiskit_dict = qiskit.__qiskit_version__
    logfile.write("qiskit version \n")
    for k in qiskit_dict:
    	logfile.write(k+" : "+qiskit_dict[k]+"\n")
    logfile.write("\n\n")

    logfile.write("local run options \n")
    for k in opt:
    	logfile.write(k+" : "+str(opt[k])+"\n")
    logfile.write("\n\n")

    import time
    t0 = time.time()

    if(opt["input_file"] is None):
     # ----- normal molecular calc ----- #
     if(opt["calc_type"].lower() == "rhf"):  hf=HFMethodType.RHF
     if(opt["calc_type"].lower() == "rohf"): hf=HFMethodType.ROHF
     if(opt["calc_type"].lower() == "uhf"):  hf=HFMethodType.UHF
     driver      = PySCFDriver(atom=opt["geometry"],unit=UnitsType.ANGSTROM,charge=opt["charge"],spin=opt["spin"],basis=opt["basis"],hf_method=hf)
     molecule    = driver.run()
     molecule_ec = QMolecule_ec(molecule=molecule,filename=None,logfile=logfile)
    else:
     # ----- custom matrix elements ----- #
     molecule_ec = QMolecule_ec(molecule=None,filename=opt["input_file"],calc=opt["calc_type"],nfreeze=opt["nfreeze"],logfile=logfile)

    molecule_ec.mo_eri_ints_ec = (molecule_ec.mo_eri_ints_ec).transpose((0,1,3,2))

    t1 = time.time()
    logfile.write("classical ES and setup: %f [s] \n" % (t1-t0))
    logfile.write("\n\n")

    core        = Hamiltonian(qubit_mapping=QubitMappingType.PARITY,two_qubit_reduction=True,freeze_core=False)
    qubit_op, _ = core.run(molecule_ec)

    t2 = time.time()
    logfile.write("second-quantized Hamiltonian setup : %f [s] \n" % (t2-t1))
    logfile.write("\n\n")

    logfile.write("Original number of qubits %d \n" % (qubit_op.num_qubits))
    z2_symmetries = Z2Symmetries.find_Z2_symmetries(qubit_op)

    nsym = len(z2_symmetries.sq_paulis)
    the_tapered_op = qubit_op
    sqlist = None
    z2syms = None

    if(nsym>0):

       logfile.write('\nZ2 symmetries found: \n')
       for symm in z2_symmetries.symmetries:
           logfile.write(symm.to_label())
       logfile.write('\nsingle-qubit operators found: \n')
       for sq in z2_symmetries.sq_paulis:
           logfile.write(sq.to_label())
       logfile.write('\nCliffords found: \n')
       for clifford in z2_symmetries.cliffords:
           logfile.write(clifford.print_details())
       logfile.write('\nsingle-qubit list: {} \n'.format(z2_symmetries.sq_list))
       
       tapered_ops = z2_symmetries.taper(qubit_op)
       for tapered_op in tapered_ops:
           logfile.write("Number of qubits of tapered qubit operator: %d \n" % (tapered_op.num_qubits))
       
       t3 = time.time()
       logfile.write("detection of symmetries: %f [s] \n" % (t3-t2))
     
       smallest_eig_value = 99999999999999
       smallest_idx = -1
      
       for idx in range(len(tapered_ops)):
           td0 = time.time()
           from utils import retrieve_SCF_energy
           print("In sector ",idx,len(tapered_ops))
           curr_value = retrieve_SCF_energy(tapered_ops[idx].copy(),core,opt) #,parm_list)
           curr_value = np.abs(curr_value-molecule_ec.hf_energy)
           print("Deviation ",curr_value)
           if curr_value < smallest_eig_value:
              smallest_eig_value = curr_value
              smallest_idx = idx
              if(curr_value<1e-6): break
           td1 = time.time()
           val = curr_value
           logfile.write("Lowest-energy computational basis state of the {}-th tapered operator is %s %f \n" % (str(idx),val))
           logfile.write("HF search time %f: \n" % (td1-td0))
     
       the_tapered_op = tapered_ops[smallest_idx]
       the_coeff = tapered_ops[smallest_idx].z2_symmetries.tapering_values
       logfile.write("{}-th tapered operator, with corresponding symmetry sector of {}".format(smallest_idx, the_coeff))
       logfile.write("\nNumber of qubits in the {}-th tapered operator {}\n\n".format(smallest_idx,the_tapered_op.num_qubits))

       sqlist = the_tapered_op.z2_symmetries.sq_list
       z2syms = the_tapered_op.z2_symmetries

    # ========
    # setup initial state
    # ========

    init_state = HartreeFock(num_qubits=the_tapered_op.num_qubits, num_orbitals=core._molecule_info['num_orbitals'],
                        qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction,
                        num_particles=core._molecule_info['num_particles'],sq_list=sqlist)

    # ---- initial parameter guess 
    init_parm = None
    if(opt["start_pt"].lower()=="file"):
       init_parm = np.loadtxt('vqe.parameters')
    if(opt["var_form"].lower()=="uccsd" and opt["start_pt"].lower()=="ccsd"):
       parm_list    = build_parameter_list(molecule_ec)

    logfile.write("Initial parameters = %d\n" % len(parm_list))
    for mu,p in enumerate(parm_list):
        logfile.write('%d %f\n' % (mu,p))

    if(opt["var_form"].lower()=="uccsd"):
        var_form = UCCSD(num_qubits=the_tapered_op.num_qubits,depth=opt["UCCSD_depth"],
                   num_orbitals=core._molecule_info['num_orbitals'],
                   num_particles=core._molecule_info['num_particles'],
                   active_occupied=opt["UCCSD_active_occupied"], active_unoccupied=opt["UCCSD_active_unoccupied"], initial_state=init_state,
                   qubit_mapping=core._qubit_mapping, two_qubit_reduction=core._two_qubit_reduction,
                   num_time_slices=opt["UCCSD_num_time_slices"],z2_symmetries=z2syms,init_parm=parm_list)

        if(opt["var_form"].lower()=="uccsd" and opt["start_pt"].lower()=="ccsd"):
           nparm,ndepth = len(var_form._mask),var_form._depth
           init_parm    = np.zeros(nparm*ndepth)
           for idp in range(ndepth):
            for ims in range(nparm):
             init_parm[ims+idp*nparm] = parm_list[var_form._mask[ims]]

        logfile.write("Selected parameters = %d\n" % nparm)
        for mu,p in enumerate(var_form._mask):
            logfile.write('%d %f\n' % (p,parm_list[p]))

    elif(opt["var_form"].lower()=="ry"):
        var_form = RY(the_tapered_op.num_qubits,depth=opt["R_depth"],
                   entanglement=opt["R_entanglement"],initial_state=HF_state)
    elif(opt["var_form"].lower()=="ryrz"):
        var_form = RYRZ(the_tapered_op.num_qubits,depth=opt["R_depth"],
                   entanglement=opt["R_entanglement"],initial_state=HF_state)
    elif(opt["var_form"].lower()=="swaprz"):
        var_form = SwapRZ(the_tapered_op.num_qubits,depth=opt["R_depth"],
                   entanglement=opt["R_entanglement"],initial_state=HF_state)
    else:
        print("invalid variational form")
        assert(False)

    # setup optimizer
    if(  opt["optimizer"].lower()=="bfgs"):  optimizer = L_BFGS_B(maxiter=opt["max_eval"])
    elif(opt["optimizer"].lower()=="cg"):    optimizer = CG(maxiter=opt["max_eval"])
    elif(opt["optimizer"].lower()=="slsqp"): optimizer = SLSQP(maxiter=opt["max_eval"])
    elif(opt["optimizer"].lower()=="spsa"):  optimizer = SPSA()
    elif(opt["optimizer"].lower()=="cobyla"): optimizer = COBYLA(maxiter=opt["max_eval"])
    else:                                    print("not coded yet"); assert(False) 

    # set vqe
    if(opt["var_form"].lower()=="uccsd"): algo = VQE(the_tapered_op,var_form,optimizer,initial_point=init_parm)
    else:                                 algo = VQE(the_tapered_op,var_form,optimizer)
    # setup backend
    backend = Aer.get_backend('statevector_simulator')
    quantum_instance = QuantumInstance(backend=backend)

    t0 = time.time()

    algo_result = algo.run(quantum_instance)

    t1 = time.time()

    logfile.write("\nVQE time [s] %f \n\n" % (t1-t0))

    result = core.process_algorithm_result(algo_result)
    for line in result[0]:
        logfile.write(line+"\n")
    
    logfile.write("\nThe parameters for UCCSD are:\n")
    for i,(tc,tq) in enumerate(zip(init_parm,algo_result['opt_params'])):
     logfile.write("%d %f %f \n" % (i,tc,tq))

    if(opt["print_parameters"]):
       par_file = open('vqe.parameters','w')
       for p in algo_result['opt_params']:
        par_file.write("%f \n" % p)
       par_file.close()

    #td0 = time.time()
    #ee = davidson(the_tapered_op,'fci')
    #td1 = time.time()
    #logfile.write("\n\nExact diagonalization, energy: %f \n" % (ee+molecule_ec.nuclear_repulsion_energy+molecule_ec.energy_offset_ec))

    #logfile.write("Davidson FCI time: %f [s] \n" % (td1-td0))
    
    print('============================================================================')
    print('                                      DONE!')
    print('============================================================================')
    

    return 0