def execute(self, inputParams): xacc_opts = inputParams['XACC'] acc_name = xacc_opts['accelerator'] 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 'Circuit' not in inputParams: xacc.error( 'Invalid benchmark input - must have circuit description') self.qpu = xacc.getAccelerator(acc_name, xacc_opts) if 'Decorators' in inputParams: if 'readout_error' in inputParams['Decorators']: qpu = xacc.getAcceleratorDecorator('ro-error', qpu) provider = xacc.getIRProvider('quantum') if 'source' in inputParams['Circuit']: # here assume this is xasm always src = inputParams['Circuit']['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] self.circuit_name = circuit_name ansatz = xacc.getCompiled(circuit_name) opts = {'circuit': ansatz, 'accelerator': self.qpu} if 'qubit-map' in inputParams['Circuit']: raw_qbit_map = inputParams['Circuit']['qubit-map'] if not isinstance(raw_qbit_map, list): raw_qbit_map = ast.literal_eval(raw_qbit_map) self.qubit_map = raw_qbit_map opts['qubit-map'] = self.qubit_map self.qpt = xacc.getAlgorithm('qpt', opts) self.nq = ansatz.nLogicalBits() buffer = xacc.qalloc(ansatz.nLogicalBits()) self.qpt.execute(buffer) return buffer
def initialize(self, options): config = open(os.getenv('HOME')+'/.dwave_config', 'r').read() lines = config.split('\n') for l in lines: if 'key' in l: self.token = l.split(':')[1].strip() if 'shots' in options: self.shots = options['shots'] if 'backend' in options: self.backend = options['backend'] else: xacc.error('[Dwave Backend] Must set backend.') if 'chain_strength' in options: self.chain_strength = options['chain_strength']
def XACC2QubitOperator(pauli_op): try: from openfermion import QubitOperator except: xacc.error( "OpenFermion not installed, cannot convert PauliOperator to QubitOperator." ) return qop = QubitOperator() for o in pauli_op: term = tuple(o[1].ops().items()) if term == () or term[0][1] == 'I': qop += QubitOperator((), o[1].coeff()) else: qop += QubitOperator(tuple(o[1].ops().items()), o[1].coeff()) return qop
def execute(self, buffer, program): import neal from collections import Counter counter = 0 h = {} J={} Q={} for inst in program.getInstructions(): if inst.bits()[0] == inst.bits()[1]: h[counter] = inst.getParameter(0) Q[(counter, counter)] = inst.getParameter(0) counter+=1 else: J[(inst.bits()[0], inst.bits()[1])] = inst.getParameter(0) Q[(inst.bits()[0], inst.bits()[1])] = inst.getParameter(0) sampler = neal.SimulatedAnnealingSampler() if self.mode == 'ising': response = sampler.sample_ising(h, J, num_reads=self.shots) elif self.mode == 'qubo': response = sampler.sample_qubo(Q, num_reads = self.shots) else: xacc.error('[dwave-neal] invalid execution mode: ' + self.mode) energies = [d.energy for d in response.data(['energy'])] counts_list = [] unique_config_to_energy = {} for i, sample in enumerate(response): l = list(sample.values()) st = '' for li in l: st += '1' if li == 1 else '0' counts_list.append(st) if not st in unique_config_to_energy: unique_config_to_energy[st] = energies[i] counts = Counter(counts_list) buffer.setMeasurements(counts) buffer.addExtraInfo('unique-configurations', unique_config_to_energy) gstate = min(unique_config_to_energy, key=unique_config_to_energy.get) buffer.addExtraInfo('ground_state', gstate)
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