def generate(self, inputParams): src = """0.7080240949826064 - 1.248846801817026 0^ 0 - 1.248846801817026 1^ 1 - 0.4796778151607899 2^ 2 - 0.4796778151607899 3^ 3 + 0.33667197218932576 0^ 1^ 1 0 + 0.0908126658307406 0^ 1^ 3 2 + 0.09081266583074038 0^ 2^ 0 2 + 0.331213646878486 0^ 2^ 2 0 + 0.09081266583074038 0^ 3^ 1 2 + 0.331213646878486 0^ 3^ 3 0 + 0.33667197218932576 1^ 0^ 0 1 + 0.0908126658307406 1^ 0^ 2 3 + 0.09081266583074038 1^ 2^ 0 3 + 0.331213646878486 1^ 2^ 2 1 + 0.09081266583074038 1^ 3^ 1 3 + 0.331213646878486 1^ 3^ 3 1 + 0.331213646878486 2^ 0^ 0 2 + 0.09081266583074052 2^ 0^ 2 0 + 0.331213646878486 2^ 1^ 1 2 + 0.09081266583074052 2^ 1^ 3 0 + 0.09081266583074048 2^ 3^ 1 0 + 0.34814578469185886 2^ 3^ 3 2 + 0.331213646878486 3^ 0^ 0 3 + 0.09081266583074052 3^ 0^ 2 1 + 0.331213646878486 3^ 1^ 1 3 + 0.09081266583074052 3^ 1^ 3 1 + 0.09081266583074048 3^ 2^ 0 1 + 0.34814578469185886 3^ 2^ 2 3""" inputParams['rdm-source'] = src return xacc.getObservable('fermion', src)
def runVqeGradientDescent(gradientStrategy): # Get access to the desired QPU and # allocate some qubits to run on qpu = xacc.getAccelerator('qpp') # Construct the Hamiltonian as an XACC PauliOperator ham = xacc.getObservable('pauli', '1.0 Y0') # Ansatz circuit: xacc.qasm('''.compiler xasm .circuit ansatz .qbit q .parameters t0, t1, t2, t3 // State-prep Ry(q[0], pi/4); Ry(q[1], pi/3); Ry(q[2], pi/7); // Parametrized gates (layer 1) Rz(q[0], t0); Rz(q[1], t1); CX(q[0], q[1]); CX(q[1], q[2]); // Parametrized gates (layer 2) Ry(q[1], t2); Rx(q[2], t3); CX(q[0], q[1]); CX(q[1], q[2]); ''') # We need 4 qubits buffer = xacc.qalloc(4) ansatz_circuit = xacc.getCompiled('ansatz') initParams = [0.432, -0.123, 0.543, 0.233] opt = xacc.getOptimizer( 'mlpack', { # Using gradient descent: 'mlpack-optimizer': 'gd', 'initial-parameters': initParams, 'mlpack-step-size': 0.01, 'mlpack-max-iter': 200 }) # Create the VQE algorithm with the requested gradient strategy vqe = xacc.getAlgorithm( 'vqe', { 'ansatz': ansatz_circuit, 'accelerator': qpu, 'observable': ham, 'optimizer': opt, 'gradient_strategy': gradientStrategy }) vqe.execute(buffer) energies = buffer.getInformation('params-energy') return energies
# Simple 1-qubit demonstration of the Quatum Imaginary Time Evolution/QLanczos algorithm # Reference: https://www.nature.com/articles/s41567-019-0704-4 # Target H = 1/sqrt(2)(X + Z) # Expected minimum value: -1.0 import xacc, sys, numpy as np import matplotlib.pyplot as plt # Get access to the desired QPU and # allocate some qubits to run on qpu = xacc.getAccelerator('qpp') # Construct the Hamiltonian as an XACC PauliOperator ham = xacc.getObservable('pauli', '0.70710678118 X0 + 0.70710678118 Z0') # See Fig. 2 (e) of https://www.nature.com/articles/s41567-019-0704-4 # Horizontal axis: 0 -> 2.5 # The number of Trotter steps nbSteps = 25 # The Trotter step size stepSize = 0.1 # Create the QITE algorithm qite = xacc.getAlgorithm('qite', { 'accelerator': qpu, 'observable': ham, 'step-size': stepSize, 'steps': nbSteps }) # We just need 1 qubit qiteBuffer = xacc.qalloc(1)
import xacc import sys import numpy as np # Get access to the desired QPU and # allocate some qubits to run on qpu = xacc.getAccelerator('tnqvm', {'vqe-mode': True}) buffer = xacc.qalloc(4) nahHamStr = '(0.0816923,0) Z2 Z3 + (0.11747,0) Z1 Z3 + (-0.00944179,0) Z1 Y2 Y3 + (0.101934,0) Z0 Z3 + (0.0229208,0) Y0 Y1 Z2 + (0.158901,0) Z0 Z2 + (0.0229208,0) Z0 X2 X3 + (-0.387818,0) Z3 + (0.0323625,0) X2 X3 + (0.0816923,0) Z0 Z1 + (-0.00944179,0) Z1 X2 X3 + (-0.00944179,0) X0 X1 Z3 + (0.0323625,0) Y2 Y3 + (0.0202421,0) X0 X1 Y2 Y3 + (0.0149387,0) Z0 + (0.0149387,0) Z2 + (-0.387818,0) Z1 + (0.0323625,0) Y0 Y1 + (-0.00944179,0) Y0 Y1 Z3 + (0.0229208,0) X0 X1 Z2 + (0.101934,0) Z1 Z2 + (0.0323625,0) X0 X1 + (0.0229208,0) Z0 Y2 Y3 + (-159.403,0) + (0.0202421,0) Y0 Y1 Y2 Y3 + (0.0202421,0) X0 X1 X2 X3 + (0.0202421,0) Y0 Y1 X2 X3' ham = xacc.getObservable('pauli', nahHamStr) @xacc.qpu(algo='vqe', accelerator=qpu, observable=ham) def ansatz(q, x): X(q[0]) X(q[2]) ucc3(q, x[0], x[1], x[2]) ansatz(buffer, [0.0, 0.0, 0.0]) print('Energy = ', buffer.getInformation('opt-val')) print('Opt Angles = ', buffer.getInformation('opt-params'))
def execute(self, inputParams): xacc_opts = inputParams['XACC'] acc_name = xacc_opts['accelerator'] qpu = xacc.getAccelerator(acc_name, xacc_opts) if 'verbose' in xacc_opts and xacc_opts['verbose']: xacc.set_verbose(True) if 'Benchmark' not in inputParams: xacc.error('Invalid benchmark input - must have Benchmark description') if 'Observable' not in inputParams: xacc.error('Invalid benchmark input - must have Observable description') if 'Ansatz' not in inputParams: xacc.error('Invalid benchmark input - must have Ansatz circuit description') if 'Decorators' in inputParams: if 'readout_error' in inputParams['Decorators']: qpu = xacc.getAcceleratorDecorator('ro-error', qpu) H = None if inputParams['Observable']['name'] == 'pauli': obs_str = inputParams['Observable']['obs_str'] H = xacc.getObservable('pauli', obs_str) elif inputParams['Observable']['name'] == 'fermion': obs_str = inputParams['Observable']['obs_str'] H = xacc.getObservable('fermion', obs_str) elif inputParams['Observable']['name'] == 'psi4': opts = {'basis':inputParams['Observable']['basis'], 'geometry':inputParams['Observable']['geometry']} if 'fo' in inputParams['Observable'] and 'ao' in inputParams['Observable']: opts['frozen-spin-orbitals'] = ast.literal_eval(inputParams['Observable']['fo']) opts['active-spin-orbitals'] = ast.literal_eval(inputParams['Observable']['ao']) H = xacc.getObservable('psi4', opts) #print('Ham: ', H.toString()) buffer = xacc.qalloc(H.nBits()) optimizer = None if 'Optimizer' in inputParams: # check that values that can be ints/floats are opts = inputParams['Optimizer'] for k,v in inputParams['Optimizer'].items(): try: i = int(v) opts[k] = i continue except: pass try: f = float(v) opts[k] = f continue except: pass optimizer = xacc.getOptimizer(inputParams['Optimizer']['name'] if 'Optimizer' in inputParams else 'nlopt', opts) else: optimizer = xacc.getOptimizer('nlopt') provider = xacc.getIRProvider('quantum') if 'source' in inputParams['Ansatz']: # here assume this is xasm always src = inputParams['Ansatz']['source'] xacc.qasm(src) # get the name of the circuit circuit_name = None for l in src.split('\n'): if '.circuit' in l: circuit_name = l.split(' ')[1] ansatz = xacc.getCompiled(circuit_name) else: ansatz = provider.createInstruction(inputParams['Ansatz']['ansatz']) ansatz = xacc.asComposite(ansatz) alg = xacc.getAlgorithm(inputParams['Benchmark']['algorithm'], { 'ansatz': ansatz, 'accelerator': qpu, 'observable': H, 'optimizer': optimizer, }) alg.execute(buffer) return buffer
import xacc xacc.set_verbose(True) accelerator = xacc.getAccelerator("qpp") buffer = xacc.qalloc(4) optimizer = xacc.getOptimizer('nlopt', {'nlopt-optimizer': 'l-bfgs'}) pool = "multi-qubit-qaoa" nLayers = 2 subAlgo_qaoa = "QAOA" H = xacc.getObservable( 'pauli', '-5.0 - 0.5 Z0 - 1.0 Z2 + 0.5 Z3 + 1.0 Z0 Z1 + 2.0 Z0 Z2 + 0.5 Z1 Z2 + 2.5 Z2 Z3' ) adapt = xacc.getAlgorithm( 'adapt', { 'accelerator': accelerator, 'observable': H, 'optimizer': optimizer, 'pool': pool, 'maxiter': nLayers, 'sub-algorithm': subAlgo_qaoa }) adapt.execute(buffer)
# Simple 3-qubit demonstration of the Quatum Imaginary Time Evolution algorithm # for Deuteron H3 Hamiltonian # Reference: https://arxiv.org/pdf/1912.06226.pdf # Expected minimum value: -2.04482 import xacc, sys, numpy as np import matplotlib.pyplot as plt # Get access to the desired QPU and # allocate some qubits to run on qpu = xacc.getAccelerator('qpp') # Construct the H3 Hamiltonian ham = xacc.getObservable( 'pauli', '5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1 + 9.625 - 9.625 Z2 - 3.91 X1 X2 - 3.91 Y1 Y2' ) xacc.qasm('''.compiler xasm .circuit prep .qbit q X(q[0]); ''') prep_circuit = xacc.getCompiled('prep') # We need 3 qubits for this case (H3) buffer = xacc.qalloc(3) # Horizontal axis: 0 -> 0.3 (step = 0.05) # The number of Trotter steps nbSteps = 6
qpu = xacc.getAccelerator('tnqvm', {'vqe-mode': True}) buffer = xacc.qalloc(4) geom = ''' 0 1 Na 0.000000 0.0 0.0 H 0.0 0.0 1.914388 symmetry c1 ''' fo = [0, 1, 2, 3, 4, 10, 11, 12, 13, 14] ao = [5, 9, 15, 19] H = xacc.getObservable( 'psi4-frozen-core', { 'basis': 'sto-3g', 'geometry': geom, 'frozen-spin-orbitals': fo, 'active-spin-orbitals': ao }) @xacc.qpu(algo='vqe', accelerator=qpu, observable=H) def ansatz(q, x): X(q[0]) X(q[2]) ucc1(q, x[0]) print(ansatz.getFunction().toString()) print(ansatz.getFunction().getVariables())
import xacc accelerator = xacc.getAccelerator("qpp") H = xacc.getObservable( 'pauli', '0.2976 + 0.3593 Z0 - 0.4826 Z1 + 0.5818 Z0 Z1 + 0.0896 X0 X1 + 0.0896 Y0 Y1' ) expansion = 'Cioslowski' order = 2 provider = xacc.getIRProvider('quantum') ansatz = provider.createComposite('initial-state') ansatz.addInstruction(provider.createInstruction('X', [0])) buffer = xacc.qalloc(2) qcmx = xacc.getAlgorithm( 'qcmx', { 'accelerator': accelerator, 'observable': H, 'ansatz': ansatz, 'cmx-order': order, 'expansion-type': expansion }) qcmx.execute(buffer) print('Energy = ', buffer.getInformation('opt-val'))
def fromOptions(self, inputParams): import numpy as np, sys, io, os from pyscf import gto, scf, dft, tddft from pyscf.lib import logger mol = gto.mole.Mole() sys.argv = [''] mol.atom = inputParams['geometry'] mol.basis = inputParams['basis'] if 'verbose' in inputParams and inputParams['verbose']: mol.build() else: mol.build(verbose=logger.QUIET) scf_wfn = scf.RHF(mol) # needs to be changed for open-shells scf_wfn.conv_tol = 1e-8 scf_wfn.kernel() # runs RHF calculations scf_e = scf_wfn.e_tot E_nucl = mol.energy_nuc() # Get MO coefficients from SCF wavefunction # ==> ERIs <== # Create instance of MintsHelper class: nbf = mol.nao # number of basis functions nso = 2 * nbf # number of spin orbitals # Assuming RHF for now, easy to generalize later nalpha = (scf_wfn.mo_occ == 2).sum() nbeta = (scf_wfn.mo_occ == 2).sum() nocc = nalpha + nbeta # number of occupied orbitals nvirt = 2 * nbf - nocc # number of virtual orbitals list_occ_alpha = scf_wfn.mo_occ list_occ_beta = scf_wfn.mo_occ # Get orbital energies eps_a = scf_wfn.mo_energy eps_b = scf_wfn.mo_energy eps = np.append(eps_a, eps_b) # Get orbital coefficients: Ca = scf_wfn.mo_coeff Cb = scf_wfn.mo_coeff C = np.block([[Ca, np.zeros_like(Cb)], [np.zeros_like(Ca), Cb]]) # Get the two electron integrals using MintsHelper Ints = mol.intor('int2e_sph') def spin_block_tei(I): """ Function that spin blocks two-electron integrals Using np.kron, we project I into the space of the 2x2 identity, tranpose the result and project into the space of the 2x2 identity again. This doubles the size of each axis. The result is our two electron integral tensor in the spin orbital form. """ identity = np.eye(2) I = np.kron(identity, I) return np.kron(identity, I.T) # Spin-block the two electron integral array I_spinblock = spin_block_tei(Ints) # Converts chemist's notation to physicist's notation, and antisymmetrize # (pq | rs) ---> <pr | qs> # Physicist's notation tmp = I_spinblock.transpose(0, 2, 1, 3) # Antisymmetrize: # <pr||qs> = <pr | qs> - <pr | sq> gmo = tmp - tmp.transpose(0, 1, 3, 2) gmo = np.einsum('pqrs, sS -> pqrS', gmo, C) gmo = np.einsum('pqrS, rR -> pqRS', gmo, C) gmo = np.einsum('pqRS, qQ -> pQRS', gmo, C) gmo = np.einsum('pQRS, pP -> PQRS', gmo, C) # -------- 0-body term: Hamiltonian_0body = E_nucl # -------- 1-body term: # Ca* # Build core Hamiltonian T = mol.intor_symmetric('int1e_kin') V = mol.intor_symmetric('int1e_nuc') H_core_ao = T + V # -- check which one more efficient (matmul vs einsum) # H_core_mo = np.matmul(Ca.T,np.matmul(H_core_ao,Ca))) # H_core_mo = np.einsum('ij, jk, kl -> il', Ca.T, H_core_ao, Ca) H_core_mo_alpha = H_core_mo H_core_mo_beta = H_core_mo # ---- this version breaks is we permuted SCF eigvecs # Hamiltonian_1body = np.block([ # [ H_core_mo_alpha , np.zeros_like(H_core_mo_alpha)], # [np.zeros_like(H_core_mo_beta) , H_core_mo_beta ]]) # # --- th is version is safer than above (H_1b is permutted correctly if eigvecs are permutted) Hamiltonian_1body_ao = np.block([[H_core_ao, np.zeros_like(H_core_ao)], [np.zeros_like(H_core_ao), H_core_ao]]) Hamiltonian_1body = np.einsum('ij, jk, kl -> il', C.T, Hamiltonian_1body_ao, C) Hamiltonian_2body = gmo if 'frozen-spin-orbitals' in inputParams and 'active-spin-orbitals' in inputParams: MSO_frozen_list = inputParams['frozen-spin-orbitals'] MSO_active_list = inputParams['active-spin-orbitals'] n_frozen = len(MSO_frozen_list) n_active = len(MSO_active_list) else: MSO_frozen_list = [] MSO_active_list = range(Hamiltonian_1body.shape[0]) n_frozen = 0 n_active = len(MSO_active_list) # ----- 0-body frozen-core: Hamiltonian_fc_0body = E_nucl for a in range(n_frozen): ia = MSO_frozen_list[a] Hamiltonian_fc_0body += Hamiltonian_1body[ia, ia] for b in range(a): ib = MSO_frozen_list[b] Hamiltonian_fc_0body += gmo[ia, ib, ia, ib] f_str = str(Hamiltonian_fc_0body) pos_or_neg = lambda x: ' + ' if x > 0. else ' - ' # --- 1-body frozen-core: Hamiltonian_fc_1body = np.zeros((n_active, n_active)) Hamiltonian_fc_1body_tmp = np.zeros((n_active, n_active)) for p in range(n_active): ip = MSO_active_list[p] for q in range(n_active): iq = MSO_active_list[q] Hamiltonian_fc_1body[p, q] = Hamiltonian_1body[ip, iq] #Hamiltonian_fc_1body_tmp[p,q] = Hamiltonian_1body[ip,iq] for a in range(n_frozen): ia = MSO_frozen_list[a] Hamiltonian_fc_1body[p, q] += gmo[ia, ip, ia, iq] if abs(Hamiltonian_fc_1body[p, q]) > 1e-12: f_str += pos_or_neg(Hamiltonian_fc_1body[p, q]) + str( abs(Hamiltonian_fc_1body[p, q])) + ' ' + str( p) + '^ ' + str(q) # ------- 2-body frozen-core: Hamiltonian_fc_2body = np.zeros( (n_active, n_active, n_active, n_active)) for p in range(n_active): ip = MSO_active_list[p] for q in range(n_active): iq = MSO_active_list[q] for r in range(n_active): ir = MSO_active_list[r] for ss in range(n_active): iss = MSO_active_list[ss] #Hamiltonian_fc_2body[p,q,r,ss]= 0.25* gmo[ip,iq,ir,iss] Hamiltonian_fc_2body[p, q, r, ss] = gmo[ip, iq, ir, iss] #Hamiltonian_fc_2body[p,q,r,ss]= 0.25* gmo[ip,iq,iss,ir] Hamiltonian_fc_2body_tmp = 0.25 * Hamiltonian_fc_2body.transpose( 0, 1, 3, 2) for p in range(n_active): ip = MSO_active_list[p] for q in range(n_active): iq = MSO_active_list[q] for r in range(n_active): ir = MSO_active_list[r] for ss in range(n_active): if abs(Hamiltonian_fc_2body_tmp[p, q, r, ss]) > 1e-12: f_str += pos_or_neg( Hamiltonian_fc_2body_tmp[p, q, r, ss]) + str( abs(Hamiltonian_fc_2body_tmp[p, q, r, ss]) ) + ' ' + str(p) + '^ ' + str(q) + '^ ' + str( r) + ' ' + str(ss) self.observable = xacc.getObservable('fermion', f_str) self.asPauli = xacc.transformToPauli('jw', self.observable)
def fromOptions(self, inputParams): import numpy as np import psi4 psi4.core.be_quiet() g = inputParams['geometry'] basis = inputParams['basis'] moleculeGeom = psi4.geometry(g) psi4.set_options({ 'basis': basis, 'scf_type': 'pk', 'mp2_type': 'conv', 'e_convergence': 1e-8, 'd_convergence': 1e-8 }) scf_e, scf_wfn = psi4.energy('scf', return_wfn=True) E_nucl = moleculeGeom.nuclear_repulsion_energy() # Get MO coefficients from SCF wavefunction # ==> ERIs <== # Create instance of MintsHelper class: mints = psi4.core.MintsHelper(scf_wfn.basisset()) nbf = mints.nbf() # number of basis functions nso = 2 * nbf # number of spin orbitals nalpha = scf_wfn.nalpha() # number of alpha electrons nbeta = scf_wfn.nbeta() # number of beta electrons nocc = nalpha + nbeta # number of occupied orbitals nvirt = 2 * nbf - nocc # number of virtual orbitals list_occ_alpha = np.asarray(scf_wfn.occupation_a()) list_occ_beta = np.asarray(scf_wfn.occupation_b()) # Get orbital energies eps_a = np.asarray(scf_wfn.epsilon_a()) eps_b = np.asarray(scf_wfn.epsilon_b()) eps = np.append(eps_a, eps_b) # Get orbital coefficients: Ca = np.asarray(scf_wfn.Ca()) Cb = np.asarray(scf_wfn.Cb()) C = np.block([[Ca, np.zeros_like(Cb)], [np.zeros_like(Ca), Cb]]) # Get the two electron integrals using MintsHelper Ints = np.asarray(mints.ao_eri()) def spin_block_tei(I): """ Function that spin blocks two-electron integrals Using np.kron, we project I into the space of the 2x2 identity, tranpose the result and project into the space of the 2x2 identity again. This doubles the size of each axis. The result is our two electron integral tensor in the spin orbital form. """ identity = np.eye(2) I = np.kron(identity, I) return np.kron(identity, I.T) # Spin-block the two electron integral array I_spinblock = spin_block_tei(Ints) # Converts chemist's notation to physicist's notation, and antisymmetrize # (pq | rs) ---> <pr | qs> # Physicist's notation tmp = I_spinblock.transpose(0, 2, 1, 3) # Antisymmetrize: # <pr||qs> = <pr | qs> - <pr | sq> gao = tmp - tmp.transpose(0, 1, 3, 2) gmo = np.einsum( 'pQRS, pP -> PQRS', np.einsum( 'pqRS, qQ -> pQRS', np.einsum('pqrS, rR -> pqRS', np.einsum('pqrs, sS -> pqrS', gao, C), C), C), C) # -------- 0-body term: Hamiltonian_0body = E_nucl # -------- 1-body term: # Ca* # Build core Hamiltonian T = np.asarray(mints.ao_kinetic()) V = np.asarray(mints.ao_potential()) H_core_ao = T + V # -- check which one more efficient (matmul vs einsum) # H_core_mo = np.matmul(Ca.T,np.matmul(H_core_ao,Ca))) H_core_mo = np.einsum('ij, jk, kl -> il', Ca.T, H_core_ao, Ca) H_core_mo_alpha = H_core_mo H_core_mo_beta = H_core_mo # ---- this version breaks is we permuted SCF eigvecs # Hamiltonian_1body = np.block([ # [ H_core_mo_alpha , np.zeros_like(H_core_mo_alpha)], # [np.zeros_like(H_core_mo_beta) , H_core_mo_beta ]]) # # --- th is version is safer than above (H_1b is permutted correctly if eigvecs are permutted) Hamiltonian_1body_ao = np.block([[H_core_ao, np.zeros_like(H_core_ao)], [np.zeros_like(H_core_ao), H_core_ao]]) Hamiltonian_1body = np.einsum('ij, jk, kl -> il', C.T, Hamiltonian_1body_ao, C) Hamiltonian_2body = gmo if 'frozen-spin-orbitals' in inputParams and 'active-spin-orbitals' in inputParams: MSO_frozen_list = inputParams['frozen-spin-orbitals'] MSO_active_list = inputParams['active-spin-orbitals'] n_frozen = len(MSO_frozen_list) n_active = len(MSO_active_list) else: MSO_frozen_list = [] MSO_active_list = range(Hamiltonian_1body.shape[0]) n_frozen = 0 n_active = len(MSO_active_list) # ----- 0-body frozen-core: Hamiltonian_fc_0body = E_nucl for a in range(n_frozen): ia = MSO_frozen_list[a] Hamiltonian_fc_0body += Hamiltonian_1body[ia, ia] for b in range(a): ib = MSO_frozen_list[b] Hamiltonian_fc_0body += gmo[ia, ib, ia, ib] f_str = str(Hamiltonian_fc_0body) pos_or_neg = lambda x: ' + ' if x > 0. else ' - ' # --- 1-body frozen-core: Hamiltonian_fc_1body = np.zeros((n_active, n_active)) Hamiltonian_fc_1body_tmp = np.zeros((n_active, n_active)) for p in range(n_active): ip = MSO_active_list[p] for q in range(n_active): iq = MSO_active_list[q] Hamiltonian_fc_1body[p, q] = Hamiltonian_1body[ip, iq] #Hamiltonian_fc_1body_tmp[p,q] = Hamiltonian_1body[ip,iq] for a in range(n_frozen): ia = MSO_frozen_list[a] Hamiltonian_fc_1body[p, q] += gmo[ia, ip, ia, iq] if abs(Hamiltonian_fc_1body[p, q]) > 1e-12: f_str += pos_or_neg(Hamiltonian_fc_1body[p, q]) + str( abs(Hamiltonian_fc_1body[p, q])) + ' ' + str( p) + '^ ' + str(q) # ------- 2-body frozen-core: Hamiltonian_fc_2body = np.zeros( (n_active, n_active, n_active, n_active)) for p in range(n_active): ip = MSO_active_list[p] for q in range(n_active): iq = MSO_active_list[q] for r in range(n_active): ir = MSO_active_list[r] for ss in range(n_active): iss = MSO_active_list[ss] #Hamiltonian_fc_2body[p,q,r,ss]= 0.25* gmo[ip,iq,ir,iss] Hamiltonian_fc_2body[p, q, r, ss] = gmo[ip, iq, ir, iss] #Hamiltonian_fc_2body[p,q,r,ss]= 0.25* gmo[ip,iq,iss,ir] Hamiltonian_fc_2body_tmp = 0.25 * Hamiltonian_fc_2body.transpose( 0, 1, 3, 2) for p in range(n_active): ip = MSO_active_list[p] for q in range(n_active): iq = MSO_active_list[q] for r in range(n_active): ir = MSO_active_list[r] for ss in range(n_active): if abs(Hamiltonian_fc_2body_tmp[p, q, r, ss]) > 1e-12: f_str += pos_or_neg( Hamiltonian_fc_2body_tmp[p, q, r, ss]) + str( abs(Hamiltonian_fc_2body_tmp[p, q, r, ss]) ) + ' ' + str(p) + '^ ' + str(q) + '^ ' + str( r) + ' ' + str(ss) self.observable = xacc.getObservable('fermion', f_str) self.asPauli = xacc.transformToPauli('jw', self.observable)
import xacc # This example demonstrates how one might create # a new XACC InstructionVisitor in Python, # this demo shows a Visitor that counts the # number of each gate seen in the IR Tree. H = xacc.getObservable('pauli', '') for i,j in [[0,1], [1,2], [2,0]]: H += 0.5 * xacc.getObservable('pauli', '1.0 I - Z{} Z{}'.format(i,j)) qaoa_ansatz = xacc.createComposite('qaoa') qaoa_ansatz.expand({'nbQubits':H.nBits(), 'nbSteps':1, 'cost-ham':H, 'parameter-scheme':'Standard'}) print(qaoa_ansatz.toString()) class CountGatesOfTypePyVisitor(xacc.quantum.AllGateVisitor): def __init__(self): xacc.quantum.AllGateVisitor.__init__(self) self.count_dict = {} def visit(self, node): if node.name() in self.count_dict: self.count_dict[node.name()] += 1 else: self.count_dict[node.name()] = 1 my_visitor = CountGatesOfTypePyVisitor()
def generate(self, inputParams): src = """-159.505 - 0.714932 0^ 0 + 0.091683 0^ 1 + 0.091683 1^ 0 + 0.173443 1^ 1 - 0.714932 2^ 2 + 0.091683 2^ 3 + 0.091683 3^ 2 + 0.173443 3^ 3 - 0.0816923 0^ 1^ 0 1 + 0.0816923 0^ 1^ 1 0 - 0.158901 0^ 2^ 0 2 + 0.0229208 0^ 2^ 0 3 + 0.0229208 0^ 2^ 1 2 - 0.0202421 0^ 2^ 1 3 + 0.158901 0^ 2^ 2 0 - 0.0229208 0^ 2^ 2 1 - 0.0229208 0^ 2^ 3 0 + 0.0202421 0^ 2^ 3 1 + 0.0229208 0^ 3^ 0 2 - 0.101934 0^ 3^ 0 3 - 0.0202421 0^ 3^ 1 2 - 0.00944179 0^ 3^ 1 3 - 0.0229208 0^ 3^ 2 0 + 0.0202421 0^ 3^ 2 1 + 0.101934 0^ 3^ 3 0 + 0.00944179 0^ 3^ 3 1 + 0.0816923 1^ 0^ 0 1 - 0.0816923 1^ 0^ 1 0 + 0.0229208 1^ 2^ 0 2 - 0.0202421 1^ 2^ 0 3 - 0.101934 1^ 2^ 1 2 - 0.00944179 1^ 2^ 1 3 - 0.0229208 1^ 2^ 2 0 + 0.101934 1^ 2^ 2 1 + 0.0202421 1^ 2^ 3 0 + 0.00944179 1^ 2^ 3 1 - 0.0202421 1^ 3^ 0 2 - 0.00944179 1^ 3^ 0 3 - 0.00944179 1^ 3^ 1 2 - 0.11747 1^ 3^ 1 3 + 0.0202421 1^ 3^ 2 0 + 0.00944179 1^ 3^ 2 1 + 0.00944179 1^ 3^ 3 0 + 0.11747 1^ 3^ 3 1 + 0.158901 2^ 0^ 0 2 - 0.0229208 2^ 0^ 0 3 - 0.0229208 2^ 0^ 1 2 + 0.0202421 2^ 0^ 1 3 - 0.158901 2^ 0^ 2 0 + 0.0229208 2^ 0^ 2 1 + 0.0229208 2^ 0^ 3 0 - 0.0202421 2^ 0^ 3 1 - 0.0229208 2^ 1^ 0 2 + 0.0202421 2^ 1^ 0 3 + 0.101934 2^ 1^ 1 2 + 0.00944179 2^ 1^ 1 3 + 0.0229208 2^ 1^ 2 0 - 0.101934 2^ 1^ 2 1 - 0.0202421 2^ 1^ 3 0 - 0.00944179 2^ 1^ 3 1 - 0.0816923 2^ 3^ 2 3 + 0.0816923 2^ 3^ 3 2 - 0.0229208 3^ 0^ 0 2 + 0.101934 3^ 0^ 0 3 + 0.0202421 3^ 0^ 1 2 + 0.00944179 3^ 0^ 1 3 + 0.0229208 3^ 0^ 2 0 - 0.0202421 3^ 0^ 2 1 - 0.101934 3^ 0^ 3 0 - 0.00944179 3^ 0^ 3 1 + 0.0202421 3^ 1^ 0 2 + 0.00944179 3^ 1^ 0 3 + 0.00944179 3^ 1^ 1 2 + 0.11747 3^ 1^ 1 3 - 0.0202421 3^ 1^ 2 0 - 0.00944179 3^ 1^ 2 1 - 0.00944179 3^ 1^ 3 0 - 0.11747 3^ 1^ 3 1 + 0.0816923 3^ 2^ 2 3 - 0.0816923 3^ 2^ 3 2 """ inputParams['rdm-source'] = src return xacc.getObservable("fermion", src)
import xacc import numpy as np qpu = xacc.getAccelerator('qpp') ham = xacc.getObservable('pauli', '-5.0 - 0.5 Z0 + 1.0 Z0Z1') nbQubits = 2 steps = 1 buffer = xacc.qalloc(nbQubits) nbTotalParams = 4 # The optimizer: nlopt opt = xacc.getOptimizer('nlopt', { 'initial-parameters': np.random.rand(nbTotalParams)} ) # Create the QAOA algorithm qaoa = xacc.getAlgorithm('QAOA', { 'accelerator': qpu, 'observable': ham, 'optimizer': opt, 'steps': steps, 'parameter-scheme': 'Extend'}) # Run result = qaoa.execute(buffer) print('Min value = ', buffer.getInformation('opt-val')) print('Opt-params = ', buffer.getInformation('opt-params')) # Get the circuit qaoa_ansatz_std = xacc.createComposite('qaoa') qaoa_ansatz_std.expand({'nbQubits': nbQubits, 'nbSteps': steps, 'cost-ham': ham, 'parameter-scheme':'Extend'}) print('Extend parameterized QAOA circuit:') print(qaoa_ansatz_std)
def generate(self, inputParams): src = """-159.505 - 0.714932 0^ 0 + 0.101963 0^ 1 + 0.091683 0^ 2 + 0.101963 1^ 0 - 0.079975 1^ 1 - 0.123414 1^ 2 + 0.091683 2^ 0 - 0.123414 2^ 1 + 0.173443 2^ 2 - 0.714932 3^ 3 + 0.101963 3^ 4 + 0.091683 3^ 5 + 0.101963 4^ 3 - 0.079975 4^ 4 - 0.123414 4^ 5 + 0.091683 5^ 3 - 0.123414 5^ 4 + 0.173443 5^ 5 - 0.0597498 0^ 1^ 0 1 - 0.00895403 0^ 1^ 0 2 + 0.0597498 0^ 1^ 1 0 + 0.0140516 0^ 1^ 1 2 + 0.00895403 0^ 1^ 2 0 - 0.0140516 0^ 1^ 2 1 - 0.00895403 0^ 2^ 0 1 - 0.0816923 0^ 2^ 0 2 + 0.00895403 0^ 2^ 1 0 - 0.00421023 0^ 2^ 1 2 + 0.0816923 0^ 2^ 2 0 + 0.00421023 0^ 2^ 2 1 - 0.158901 0^ 3^ 0 3 + 0.0254908 0^ 3^ 0 4 + 0.0229208 0^ 3^ 0 5 + 0.0254908 0^ 3^ 1 3 - 0.0112333 0^ 3^ 1 4 - 0.0129455 0^ 3^ 1 5 + 0.0229208 0^ 3^ 2 3 - 0.0129455 0^ 3^ 2 4 - 0.0202421 0^ 3^ 2 5 + 0.158901 0^ 3^ 3 0 - 0.0254908 0^ 3^ 3 1 - 0.0229208 0^ 3^ 3 2 - 0.0254908 0^ 3^ 4 0 + 0.0112333 0^ 3^ 4 1 + 0.0129455 0^ 3^ 4 2 - 0.0229208 0^ 3^ 5 0 + 0.0129455 0^ 3^ 5 1 + 0.0202421 0^ 3^ 5 2 + 0.0254908 0^ 4^ 0 3 - 0.0709831 0^ 4^ 0 4 - 0.0218996 0^ 4^ 0 5 - 0.0112333 0^ 4^ 1 3 - 0.00883391 0^ 4^ 1 4 + 0.00677708 0^ 4^ 1 5 - 0.0129455 0^ 4^ 2 3 - 0.0072745 0^ 4^ 2 4 + 0.00277645 0^ 4^ 2 5 - 0.0254908 0^ 4^ 3 0 + 0.0112333 0^ 4^ 3 1 + 0.0129455 0^ 4^ 3 2 + 0.0709831 0^ 4^ 4 0 + 0.00883391 0^ 4^ 4 1 + 0.0072745 0^ 4^ 4 2 + 0.0218996 0^ 4^ 5 0 - 0.00677708 0^ 4^ 5 1 - 0.00277645 0^ 4^ 5 2 + 0.0229208 0^ 5^ 0 3 - 0.0218996 0^ 5^ 0 4 - 0.101934 0^ 5^ 0 5 - 0.0129455 0^ 5^ 1 3 + 0.00677708 0^ 5^ 1 4 - 0.00143379 0^ 5^ 1 5 - 0.0202421 0^ 5^ 2 3 + 0.00277645 0^ 5^ 2 4 - 0.00944179 0^ 5^ 2 5 - 0.0229208 0^ 5^ 3 0 + 0.0129455 0^ 5^ 3 1 + 0.0202421 0^ 5^ 3 2 + 0.0218996 0^ 5^ 4 0 - 0.00677708 0^ 5^ 4 1 - 0.00277645 0^ 5^ 4 2 + 0.101934 0^ 5^ 5 0 + 0.00143379 0^ 5^ 5 1 + 0.00944179 0^ 5^ 5 2 + 0.0597498 1^ 0^ 0 1 + 0.00895403 1^ 0^ 0 2 - 0.0597498 1^ 0^ 1 0 - 0.0140516 1^ 0^ 1 2 - 0.00895403 1^ 0^ 2 0 + 0.0140516 1^ 0^ 2 1 + 0.0140516 1^ 2^ 0 1 - 0.00421023 1^ 2^ 0 2 - 0.0140516 1^ 2^ 1 0 - 0.0821053 1^ 2^ 1 2 + 0.00421023 1^ 2^ 2 0 + 0.0821053 1^ 2^ 2 1 + 0.0254908 1^ 3^ 0 3 - 0.0112333 1^ 3^ 0 4 - 0.0129455 1^ 3^ 0 5 - 0.0709831 1^ 3^ 1 3 - 0.00883391 1^ 3^ 1 4 - 0.0072745 1^ 3^ 1 5 - 0.0218996 1^ 3^ 2 3 + 0.00677708 1^ 3^ 2 4 + 0.00277645 1^ 3^ 2 5 - 0.0254908 1^ 3^ 3 0 + 0.0709831 1^ 3^ 3 1 + 0.0218996 1^ 3^ 3 2 + 0.0112333 1^ 3^ 4 0 + 0.00883391 1^ 3^ 4 1 - 0.00677708 1^ 3^ 4 2 + 0.0129455 1^ 3^ 5 0 + 0.0072745 1^ 3^ 5 1 - 0.00277645 1^ 3^ 5 2 - 0.0112333 1^ 4^ 0 3 - 0.00883391 1^ 4^ 0 4 + 0.00677708 1^ 4^ 0 5 - 0.00883391 1^ 4^ 1 3 - 0.110544 1^ 4^ 1 4 + 0.0138874 1^ 4^ 1 5 + 0.00677708 1^ 4^ 2 3 + 0.0138874 1^ 4^ 2 4 - 0.0165034 1^ 4^ 2 5 + 0.0112333 1^ 4^ 3 0 + 0.00883391 1^ 4^ 3 1 - 0.00677708 1^ 4^ 3 2 + 0.00883391 1^ 4^ 4 0 + 0.110544 1^ 4^ 4 1 - 0.0138874 1^ 4^ 4 2 - 0.00677708 1^ 4^ 5 0 - 0.0138874 1^ 4^ 5 1 + 0.0165034 1^ 4^ 5 2 - 0.0129455 1^ 5^ 0 3 + 0.00677708 1^ 5^ 0 4 - 0.00143379 1^ 5^ 0 5 - 0.0072745 1^ 5^ 1 3 + 0.0138874 1^ 5^ 1 4 - 0.0986087 1^ 5^ 1 5 + 0.00277645 1^ 5^ 2 3 - 0.0165034 1^ 5^ 2 4 - 0.00369882 1^ 5^ 2 5 + 0.0129455 1^ 5^ 3 0 + 0.0072745 1^ 5^ 3 1 - 0.00277645 1^ 5^ 3 2 - 0.00677708 1^ 5^ 4 0 - 0.0138874 1^ 5^ 4 1 + 0.0165034 1^ 5^ 4 2 + 0.00143379 1^ 5^ 5 0 + 0.0986087 1^ 5^ 5 1 + 0.00369882 1^ 5^ 5 2 + 0.00895403 2^ 0^ 0 1 + 0.0816923 2^ 0^ 0 2 - 0.00895403 2^ 0^ 1 0 + 0.00421023 2^ 0^ 1 2 - 0.0816923 2^ 0^ 2 0 - 0.00421023 2^ 0^ 2 1 - 0.0140516 2^ 1^ 0 1 + 0.00421023 2^ 1^ 0 2 + 0.0140516 2^ 1^ 1 0 + 0.0821053 2^ 1^ 1 2 - 0.00421023 2^ 1^ 2 0 - 0.0821053 2^ 1^ 2 1 + 0.0229208 2^ 3^ 0 3 - 0.0129455 2^ 3^ 0 4 - 0.0202421 2^ 3^ 0 5 - 0.0218996 2^ 3^ 1 3 + 0.00677708 2^ 3^ 1 4 + 0.00277645 2^ 3^ 1 5 - 0.101934 2^ 3^ 2 3 - 0.00143379 2^ 3^ 2 4 - 0.00944179 2^ 3^ 2 5 - 0.0229208 2^ 3^ 3 0 + 0.0218996 2^ 3^ 3 1 + 0.101934 2^ 3^ 3 2 + 0.0129455 2^ 3^ 4 0 - 0.00677708 2^ 3^ 4 1 + 0.00143379 2^ 3^ 4 2 + 0.0202421 2^ 3^ 5 0 - 0.00277645 2^ 3^ 5 1 + 0.00944179 2^ 3^ 5 2 - 0.0129455 2^ 4^ 0 3 - 0.0072745 2^ 4^ 0 4 + 0.00277645 2^ 4^ 0 5 + 0.00677708 2^ 4^ 1 3 + 0.0138874 2^ 4^ 1 4 - 0.0165034 2^ 4^ 1 5 - 0.00143379 2^ 4^ 2 3 - 0.0986087 2^ 4^ 2 4 - 0.00369882 2^ 4^ 2 5 + 0.0129455 2^ 4^ 3 0 - 0.00677708 2^ 4^ 3 1 + 0.00143379 2^ 4^ 3 2 + 0.0072745 2^ 4^ 4 0 - 0.0138874 2^ 4^ 4 1 + 0.0986087 2^ 4^ 4 2 - 0.00277645 2^ 4^ 5 0 + 0.0165034 2^ 4^ 5 1 + 0.00369882 2^ 4^ 5 2 - 0.0202421 2^ 5^ 0 3 + 0.00277645 2^ 5^ 0 4 - 0.00944179 2^ 5^ 0 5 + 0.00277645 2^ 5^ 1 3 - 0.0165034 2^ 5^ 1 4 - 0.00369882 2^ 5^ 1 5 - 0.00944179 2^ 5^ 2 3 - 0.00369882 2^ 5^ 2 4 - 0.11747 2^ 5^ 2 5 + 0.0202421 2^ 5^ 3 0 - 0.00277645 2^ 5^ 3 1 + 0.00944179 2^ 5^ 3 2 - 0.00277645 2^ 5^ 4 0 + 0.0165034 2^ 5^ 4 1 + 0.00369882 2^ 5^ 4 2 + 0.00944179 2^ 5^ 5 0 + 0.00369882 2^ 5^ 5 1 + 0.11747 2^ 5^ 5 2 + 0.158901 3^ 0^ 0 3 - 0.0254908 3^ 0^ 0 4 - 0.0229208 3^ 0^ 0 5 - 0.0254908 3^ 0^ 1 3 + 0.0112333 3^ 0^ 1 4 + 0.0129455 3^ 0^ 1 5 - 0.0229208 3^ 0^ 2 3 + 0.0129455 3^ 0^ 2 4 + 0.0202421 3^ 0^ 2 5 - 0.158901 3^ 0^ 3 0 + 0.0254908 3^ 0^ 3 1 + 0.0229208 3^ 0^ 3 2 + 0.0254908 3^ 0^ 4 0 - 0.0112333 3^ 0^ 4 1 - 0.0129455 3^ 0^ 4 2 + 0.0229208 3^ 0^ 5 0 - 0.0129455 3^ 0^ 5 1 - 0.0202421 3^ 0^ 5 2 - 0.0254908 3^ 1^ 0 3 + 0.0112333 3^ 1^ 0 4 + 0.0129455 3^ 1^ 0 5 + 0.0709831 3^ 1^ 1 3 + 0.00883391 3^ 1^ 1 4 + 0.0072745 3^ 1^ 1 5 + 0.0218996 3^ 1^ 2 3 - 0.00677708 3^ 1^ 2 4 - 0.00277645 3^ 1^ 2 5 + 0.0254908 3^ 1^ 3 0 - 0.0709831 3^ 1^ 3 1 - 0.0218996 3^ 1^ 3 2 - 0.0112333 3^ 1^ 4 0 - 0.00883391 3^ 1^ 4 1 + 0.00677708 3^ 1^ 4 2 - 0.0129455 3^ 1^ 5 0 - 0.0072745 3^ 1^ 5 1 + 0.00277645 3^ 1^ 5 2 - 0.0229208 3^ 2^ 0 3 + 0.0129455 3^ 2^ 0 4 + 0.0202421 3^ 2^ 0 5 + 0.0218996 3^ 2^ 1 3 - 0.00677708 3^ 2^ 1 4 - 0.00277645 3^ 2^ 1 5 + 0.101934 3^ 2^ 2 3 + 0.00143379 3^ 2^ 2 4 + 0.00944179 3^ 2^ 2 5 + 0.0229208 3^ 2^ 3 0 - 0.0218996 3^ 2^ 3 1 - 0.101934 3^ 2^ 3 2 - 0.0129455 3^ 2^ 4 0 + 0.00677708 3^ 2^ 4 1 - 0.00143379 3^ 2^ 4 2 - 0.0202421 3^ 2^ 5 0 + 0.00277645 3^ 2^ 5 1 - 0.00944179 3^ 2^ 5 2 - 0.0597498 3^ 4^ 3 4 - 0.00895403 3^ 4^ 3 5 + 0.0597498 3^ 4^ 4 3 + 0.0140516 3^ 4^ 4 5 + 0.00895403 3^ 4^ 5 3 - 0.0140516 3^ 4^ 5 4 - 0.00895403 3^ 5^ 3 4 - 0.0816923 3^ 5^ 3 5 + 0.00895403 3^ 5^ 4 3 - 0.00421023 3^ 5^ 4 5 + 0.0816923 3^ 5^ 5 3 + 0.00421023 3^ 5^ 5 4 - 0.0254908 4^ 0^ 0 3 + 0.0709831 4^ 0^ 0 4 + 0.0218996 4^ 0^ 0 5 + 0.0112333 4^ 0^ 1 3 + 0.00883391 4^ 0^ 1 4 - 0.00677708 4^ 0^ 1 5 + 0.0129455 4^ 0^ 2 3 + 0.0072745 4^ 0^ 2 4 - 0.00277645 4^ 0^ 2 5 + 0.0254908 4^ 0^ 3 0 - 0.0112333 4^ 0^ 3 1 - 0.0129455 4^ 0^ 3 2 - 0.0709831 4^ 0^ 4 0 - 0.00883391 4^ 0^ 4 1 - 0.0072745 4^ 0^ 4 2 - 0.0218996 4^ 0^ 5 0 + 0.00677708 4^ 0^ 5 1 + 0.00277645 4^ 0^ 5 2 + 0.0112333 4^ 1^ 0 3 + 0.00883391 4^ 1^ 0 4 - 0.00677708 4^ 1^ 0 5 + 0.00883391 4^ 1^ 1 3 + 0.110544 4^ 1^ 1 4 - 0.0138874 4^ 1^ 1 5 - 0.00677708 4^ 1^ 2 3 - 0.0138874 4^ 1^ 2 4 + 0.0165034 4^ 1^ 2 5 - 0.0112333 4^ 1^ 3 0 - 0.00883391 4^ 1^ 3 1 + 0.00677708 4^ 1^ 3 2 - 0.00883391 4^ 1^ 4 0 - 0.110544 4^ 1^ 4 1 + 0.0138874 4^ 1^ 4 2 + 0.00677708 4^ 1^ 5 0 + 0.0138874 4^ 1^ 5 1 - 0.0165034 4^ 1^ 5 2 + 0.0129455 4^ 2^ 0 3 + 0.0072745 4^ 2^ 0 4 - 0.00277645 4^ 2^ 0 5 - 0.00677708 4^ 2^ 1 3 - 0.0138874 4^ 2^ 1 4 + 0.0165034 4^ 2^ 1 5 + 0.00143379 4^ 2^ 2 3 + 0.0986087 4^ 2^ 2 4 + 0.00369882 4^ 2^ 2 5 - 0.0129455 4^ 2^ 3 0 + 0.00677708 4^ 2^ 3 1 - 0.00143379 4^ 2^ 3 2 - 0.0072745 4^ 2^ 4 0 + 0.0138874 4^ 2^ 4 1 - 0.0986087 4^ 2^ 4 2 + 0.00277645 4^ 2^ 5 0 - 0.0165034 4^ 2^ 5 1 - 0.00369882 4^ 2^ 5 2 + 0.0597498 4^ 3^ 3 4 + 0.00895403 4^ 3^ 3 5 - 0.0597498 4^ 3^ 4 3 - 0.0140516 4^ 3^ 4 5 - 0.00895403 4^ 3^ 5 3 + 0.0140516 4^ 3^ 5 4 + 0.0140516 4^ 5^ 3 4 - 0.00421023 4^ 5^ 3 5 - 0.0140516 4^ 5^ 4 3 - 0.0821053 4^ 5^ 4 5 + 0.00421023 4^ 5^ 5 3 + 0.0821053 4^ 5^ 5 4 - 0.0229208 5^ 0^ 0 3 + 0.0218996 5^ 0^ 0 4 + 0.101934 5^ 0^ 0 5 + 0.0129455 5^ 0^ 1 3 - 0.00677708 5^ 0^ 1 4 + 0.00143379 5^ 0^ 1 5 + 0.0202421 5^ 0^ 2 3 - 0.00277645 5^ 0^ 2 4 + 0.00944179 5^ 0^ 2 5 + 0.0229208 5^ 0^ 3 0 - 0.0129455 5^ 0^ 3 1 - 0.0202421 5^ 0^ 3 2 - 0.0218996 5^ 0^ 4 0 + 0.00677708 5^ 0^ 4 1 + 0.00277645 5^ 0^ 4 2 - 0.101934 5^ 0^ 5 0 - 0.00143379 5^ 0^ 5 1 - 0.00944179 5^ 0^ 5 2 + 0.0129455 5^ 1^ 0 3 - 0.00677708 5^ 1^ 0 4 + 0.00143379 5^ 1^ 0 5 + 0.0072745 5^ 1^ 1 3 - 0.0138874 5^ 1^ 1 4 + 0.0986087 5^ 1^ 1 5 - 0.00277645 5^ 1^ 2 3 + 0.0165034 5^ 1^ 2 4 + 0.00369882 5^ 1^ 2 5 - 0.0129455 5^ 1^ 3 0 - 0.0072745 5^ 1^ 3 1 + 0.00277645 5^ 1^ 3 2 + 0.00677708 5^ 1^ 4 0 + 0.0138874 5^ 1^ 4 1 - 0.0165034 5^ 1^ 4 2 - 0.00143379 5^ 1^ 5 0 - 0.0986087 5^ 1^ 5 1 - 0.00369882 5^ 1^ 5 2 + 0.0202421 5^ 2^ 0 3 - 0.00277645 5^ 2^ 0 4 + 0.00944179 5^ 2^ 0 5 - 0.00277645 5^ 2^ 1 3 + 0.0165034 5^ 2^ 1 4 + 0.00369882 5^ 2^ 1 5 + 0.00944179 5^ 2^ 2 3 + 0.00369882 5^ 2^ 2 4 + 0.11747 5^ 2^ 2 5 - 0.0202421 5^ 2^ 3 0 + 0.00277645 5^ 2^ 3 1 - 0.00944179 5^ 2^ 3 2 + 0.00277645 5^ 2^ 4 0 - 0.0165034 5^ 2^ 4 1 - 0.00369882 5^ 2^ 4 2 - 0.00944179 5^ 2^ 5 0 - 0.00369882 5^ 2^ 5 1 - 0.11747 5^ 2^ 5 2 + 0.00895403 5^ 3^ 3 4 + 0.0816923 5^ 3^ 3 5 - 0.00895403 5^ 3^ 4 3 + 0.00421023 5^ 3^ 4 5 - 0.0816923 5^ 3^ 5 3 - 0.00421023 5^ 3^ 5 4 - 0.0140516 5^ 4^ 3 4 + 0.00421023 5^ 4^ 3 5 + 0.0140516 5^ 4^ 4 3 + 0.0821053 5^ 4^ 4 5 - 0.00421023 5^ 4^ 5 3 - 0.0821053 5^ 4^ 5 4 """ return xacc.getObservable("fermion", src)
import xacc # Get access to the desired QPU and # allocate some qubits to run on qpu = xacc.getAccelerator('tnqvm', {'vqe-mode': True}) buffer = xacc.qalloc(4) geom = ''' H 0.000000 0.0 0.0 H 0.0 0.0 .7474 ''' H = xacc.getObservable('pyscf', {'basis': 'sto-3g', 'geometry': geom}) @xacc.qpu(algo='vqe', accelerator=qpu, observable=H) def ansatz(q, x): X(q[0]) X(q[2]) ucc1(q, x[0]) ansatz(buffer, 0.0) print('Energy = ', buffer.getInformation('opt-val')) print('Opt Angles = ', buffer.getInformation('opt-params'))
(-0.174072892497,-0) 1^ 3^ 1 3 + (-0.0454063328691,-0) 0^ 2^ 1 3 + (0.120200490713,0) 0^ 1^ 1 0 + (0.0454063328691,0) 0^ 2^ 3 1 + (0.174072892497,0) 1^ 3^ 3 1 + (0.165606823582,0) 2^ 1^ 1 2 + (-0.0454063328691,-0) 2^ 1^ 3 0 + (-0.120200490713,-0) 2^ 3^ 2 3 + (0.120200490713,0) 2^ 3^ 3 2 + (-0.168335986252,-0) 0^ 2^ 0 2 + (0.120200490713,0) 3^ 2^ 2 3 + (-0.120200490713,-0) 3^ 2^ 3 2 + (0.0454063328691,0) 1^ 3^ 2 0 + (-1.2488468038,-0) 0^ 0 + (0.0454063328691,0) 3^ 1^ 0 2 + (-0.168335986252,-0) 2^ 0^ 2 0 + (0.165606823582,0) 3^ 0^ 0 3 + (-0.0454063328691,-0) 2^ 0^ 3 1 + (0.0454063328691,0) 2^ 0^ 1 3 + (-1.2488468038,-0) 2^ 2 + (0.0454063328691,0) 2^ 1^ 0 3 + (0.174072892497,0) 3^ 1^ 1 3 + (-0.479677813134,-0) 1^ 1 + (-0.174072892497,-0) 3^ 1^ 3 1 + (0.0454063328691,0) 3^ 0^ 1 2 + (-0.165606823582,-0) 3^ 0^ 3 0 + (0.0454063328691,0) 0^ 3^ 2 1 + (-0.165606823582,-0) 2^ 1^ 2 1 + (-0.120200490713,-0) 0^ 1^ 0 1 + (-0.120200490713,-0) 1^ 0^ 1 0 + (0.7080240981,0) ''' H = xacc.getObservable('fermion', opstr) adapt = xacc.getAlgorithm( 'adapt', { 'accelerator': qpu, 'optimizer': optimizer, 'observable': H, 'n-electrons': 2, 'maxiter': 2, 'sub-algorithm': 'vqe', 'pool': 'qubit-pool' }) # execute adapt.execute(buffer)
# and allocate some qubits for execution on each qpu = xacc.getAccelerator('aer', { 'shots': 1024, 'backend': 'ibmq_johannesburg', 'readout_error': True }) tnqvm = xacc.getAccelerator('tnqvm') buffer = xacc.qalloc(2) tnqvmBuffer = xacc.qalloc(2) # Turn on readout error correction by decorating # the local-ibm accelerator qpu = xacc.getAcceleratorDecorator('ro-error', qpu) # Construct the Hamiltonian ham = xacc.getObservable( 'pauli', '5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1') # Define the ansatz and decorate it to indicate # you'd like to run VQE @xacc.qpu(algo='energy', accelerator=qpu, observable=ham) def ansatz(buffer, t0): X(buffer[0]) Ry(buffer[1], t0) CNOT(buffer[1], buffer[0]) # Execute angle sweep on Local-IBM [ansatz(buffer, t) for t in np.linspace(-np.pi, np.pi, nAngles)] # Get theoretical values by overriding the
import xacc opstr = '''(0.174073,0) Z2 Z3 + (0.1202,0) Z1 Z3 + (0.165607,0) Z1 Z2 + (0.165607,0) Z0 Z3 + (0.1202,0) Z0 Z2 + (-0.0454063,0) Y0 Y1 X2 X3 + (-0.220041,0) Z3 + (-0.106477,0) + (0.17028,0) Z0 + (-0.220041,0) Z2 + (0.17028,0) Z1 + (-0.0454063,0) X0 X1 Y2 Y3 + (0.0454063,0) X0 Y1 Y2 X3 + (0.168336,0) Z0 Z1 + (0.0454063,0) Y0 X1 X2 Y3''' op = xacc.getObservable('pauli', opstr) qpu = xacc.getAccelerator('tnqvm') @xacc.qpu(algo='vqe', accelerator=qpu, observable=op) def ansatz_vqe(q, t0, t1): uccsd(q, {"ne": 2, "nq": 4}) print(ansatz_vqe.getFunction().toString()) buffer = xacc.qalloc(4) ansatz_vqe(buffer, 0., 0.)
def generate(self, inputParams): return xacc.getObservable( 'pauli', '5.907 - 2.1433 X0X1 - 2.1433 Y0Y1 + .21829 Z0 - 6.125 Z1')