def testExecuteVQE(self): src = """__qpu__ kernel() { 0.7137758743754461 -1.252477303982147 0 1 0 0 0.337246551663004 0 1 1 1 1 0 0 0 0.0906437679061661 0 1 1 1 3 0 2 0 0.0906437679061661 0 1 2 1 0 0 2 0 0.3317360224302783 0 1 2 1 2 0 0 0 0.0906437679061661 0 1 3 1 1 0 2 0 0.3317360224302783 0 1 3 1 3 0 0 0 0.337246551663004 1 1 0 1 0 0 1 0 0.0906437679061661 1 1 0 1 2 0 3 0 -1.252477303982147 1 1 1 0 0.0906437679061661 1 1 2 1 0 0 3 0 0.3317360224302783 1 1 2 1 2 0 1 0 0.0906437679061661 1 1 3 1 1 0 3 0 0.3317360224302783 1 1 3 1 3 0 1 0 0.3317360224302783 2 1 0 1 0 0 2 0 0.0906437679061661 2 1 0 1 2 0 0 0 0.3317360224302783 2 1 1 1 1 0 2 0 0.0906437679061661 2 1 1 1 3 0 0 0 -0.4759344611440753 2 1 2 0 0.0906437679061661 2 1 3 1 1 0 0 0 0.3486989747346679 2 1 3 1 3 0 2 0 0.3317360224302783 3 1 0 1 0 0 3 0 0.0906437679061661 3 1 0 1 2 0 1 0 0.3317360224302783 3 1 1 1 1 0 3 0 0.0906437679061661 3 1 1 1 3 0 1 0 0.0906437679061661 3 1 2 1 0 0 1 0 0.3486989747346679 3 1 2 1 2 0 3 0 -0.4759344611440753 3 1 3 0 }""" op = vqe.compile(src) self.assertAlmostEqual(vqe.execute(op, **{'task':'vqe', 'n-electrons':2}).energy, -1.13727, places=5)
def testExecuteDiagonalize(self): src = """__qpu__ kernel() { 0.7137758743754461 -1.252477303982147 0 1 0 0 0.337246551663004 0 1 1 1 1 0 0 0 0.0906437679061661 0 1 1 1 3 0 2 0 0.0906437679061661 0 1 2 1 0 0 2 0 0.3317360224302783 0 1 2 1 2 0 0 0 0.0906437679061661 0 1 3 1 1 0 2 0 0.3317360224302783 0 1 3 1 3 0 0 0 0.337246551663004 1 1 0 1 0 0 1 0 0.0906437679061661 1 1 0 1 2 0 3 0 -1.252477303982147 1 1 1 0 0.0906437679061661 1 1 2 1 0 0 3 0 0.3317360224302783 1 1 2 1 2 0 1 0 0.0906437679061661 1 1 3 1 1 0 3 0 0.3317360224302783 1 1 3 1 3 0 1 0 0.3317360224302783 2 1 0 1 0 0 2 0 0.0906437679061661 2 1 0 1 2 0 0 0 0.3317360224302783 2 1 1 1 1 0 2 0 0.0906437679061661 2 1 1 1 3 0 0 0 -0.4759344611440753 2 1 2 0 0.0906437679061661 2 1 3 1 1 0 0 0 0.3486989747346679 2 1 3 1 3 0 2 0 0.3317360224302783 3 1 0 1 0 0 3 0 0.0906437679061661 3 1 0 1 2 0 1 0 0.3317360224302783 3 1 1 1 1 0 3 0 0.0906437679061661 3 1 1 1 3 0 1 0 0.0906437679061661 3 1 2 1 0 0 1 0 0.3486989747346679 3 1 2 1 2 0 3 0 -0.4759344611440753 3 1 3 0 }""" op = vqe.compile(src) self.assertEqual(vqe.execute(op).energy, -1.137270422069755)
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( op, **{ 'mpi-provider': 'no-mpi', 'task': 'compute-energy', 'vqe-params': str(params[0]) + ',' + str(params[1]) }).energy
if k.nInstructions() > 0: evaledAnsatz = vqe.AnsatzEvaluator.evaluate( ansatz3x3, 2, np.asarray(params)) k.insertInstruction(0, evaledAnsatz) qpu.execute(qubits, k) exp = qubits.getExpectationValueZ() qubits.resetBuffer() val = coeff * exp else: val = coeff energy += val return energy.real print('XACC Diagonalize: ', vqe.execute(hamiltonian3x3, **{}).energy) #'task':'vqe', 'ansatz':ansatz3x3}).energy) print( 'XACC Nelder-Mead: ', vqe.execute(hamiltonian3x3, **{ 'task': 'vqe', 'vqe-params': '.7,.2', 'ansatz': ansatz3x3 }).energy) print('XACC SciPy Minimze:\n', minimize(energy, [0, 0])) from bayes_opt import BayesianOptimization def neg_energy(x, y): return -1 * energy([x, y])
ham = PauliOperator(5.906709445) + PauliOperator({ 0: 'X', 1: 'X' }, -2.1433) + PauliOperator({ 0: 'Y', 1: 'Y' }, -2.1433) + PauliOperator({0: 'Z'}, .21829) + PauliOperator({1: 'Z'}, -6.125) print('\nH_{2x2} = ', ham) src = """__qpu__ f(AcceleratorBuffer ab, double t0) { X 0 RY(t0) 1 CNOT 1 0 }""" p = xacc.Program(xacc.getAccelerator('tnqvm'), src) print('Program built python') k = p.getKernel('f') print('Got Kernel') ansatz = k.getIRFunction() print('Ansztz = ', ansatz.name()) print('Ansatz = \n', ansatz.toString('q')) angle_range = np.linspace(-np.pi, np.pi, 50) vqeResult = vqe.execute(ham, **{'task': 'vqe', 'ansatz': ansatz}) print('(Optimal Angle, Energy) = (', vqeResult.angles, ',', vqeResult.energy, ')') print('Number of QPU Calls = ', vqeResult.nQpuCalls) print('Number of VQE Iterations = ', vqeResult.vqeIterations)