file.write('{} '.format(str(gate) + '(' + str(angle) + ')') + str(j) + '\n') else: file.write('{} '.format(str(gate)) + str(j) + ' \n') for e in evens: file.write('CNOT {0} {1}\n'.format(*e)) for e in odds: file.write('CNOT {0} {1}\n'.format(*e)) file.write("}") file.close() return open(file_name).read() @profile def execute(nq, rounds): src = write_qasm(rounds, nq) qpu = xacc.getAccelerator('tnqvm') xacc.setOption('tnqvm-verbose', '') f = xacc.compileKernel(qpu, src) buf = qpu.createBuffer('q', nq) qpu.execute(buf, f) if __name__ == '__main__': opts = parse_args(sys.argv[1:]) xacc.Initialize(['--compiler', 'quil']) execute(opts.n, opts.r)
import pyxacc as xacc src = """__qpu__ variable(AcceleratorBuffer ab, double ta, double tp, double tq, double h, double J) { anneal ta tp tq; 0 0 h; 1 1 h; 0 1 J; }""" xacc.Initialize(['--compiler', 'dwave-qmi']) qpu = xacc.getAccelerator('dwave') qubits = qpu.createBuffer('qbits') p = xacc.Program(qpu, src) p.build() variable = p.getKernel('variable') for t in [20., 30.]: variable.execute(qubits, [t, 0., 0., 2., 3.]) xacc.Finalize()
import pyxaccvqe as vqe import pyxacc as xacc import numpy as np from stochopy import Evolutionary from mpi4py import MPI # Initialize MPI and XACC mpi_comm = MPI.COMM_WORLD mpi_rank = mpi_comm.Get_rank() xacc.Initialize() xacc.setOption('n-electrons', '2') op = vqe.compile("""&FCI NORB= 2,NELEC= 2,MS2= 0, ORBSYM=1,5, ISYM=0, / i a j b 0.6744931033260081E+00 1 1 1 1 0.6634720448605567E+00 2 2 1 1 0.6973979494693358E+00 2 2 2 2 0.1812875358123322E+00 2 1 2 1 -0.1252477303982147E+01 1 1 0 0 -0.4759344611440753E+00 2 2 0 0 0.7137758743754461E+00 0 0 0 0 """) # Define PSO objective function def f(params): # note 'no-mpi' at the compute energy, hamiltonian term # level, but we are using mpi at the higher PSO swarm level return vqe.execute(
def main(argv=None): # Store results to this CSV file file = open('projectq_out.csv', 'w') file.write('theta, Z0_pq, Z0_tnqvm\n') #, Z1, Z0Z1\n') # Initialize XACC xacc.Initialize() # Indicate that we want to use the ProjectQ QASM # Compiler/Transpiler xacc.setOption('compiler', 'projectq-qasm') # Get reference to the TNQVM Accelerator tnqvm = xacc.getAccelerator('tnqvm') # Allocate and AcceleratorBuffer xacc_qbits = tnqvm.createBuffer('qreg', 2) # Loop over H2 state prep variational parameters for theta in np.linspace(-np.pi, np.pi, 100): # Create a ProjectQ Engine with our CommandPrinter output = StringIO.StringIO() eng = MainEngine(Simulator(), [XaccCommandPrinter(output)]) # Allocate some ProjectQ qubits qreg = eng.allocate_qureg(2) # Run Init State Circuit and Generate the Z0 QubitOperator op = Z0Term(qreg, theta) eng.flush() # Get Expectation Value of Z0 operator e_pq = eng.backend.get_expectation_value(op, qreg) Measure | qreg[0] # Get the ProjectQ QASM qasm = output.getvalue() # Generate an XACC Kernel xaccKernel = getXACCKernel(qasm, tnqvm) # Execute, no params since theta has # already been input to Z0Term function xaccKernel.execute(xacc_qbits, []) # Get the expectation value e_tnqvm = xacc_qbits.getExpectationValueZ() # Reset the qubits for the next iteration xacc_qbits.resetBuffer() # Store the results to a CSV file file.write(str(theta) + ', ' + str(e_pq) + ', ' + str(e_tnqvm) + '\n') file.flush() file.close() # Finalize the framework xacc.Finalize()