Unverified Commit 4e65d738 authored by Mccaskey, Alex's avatar Mccaskey, Alex Committed by GitHub
Browse files

Merge pull request #128 from zpparks314/zach_vqe

Zach vqe
parents 9616680b 94a83bbd
from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provides, \
Validate, Invalidate, Instantiate
from ansatzgenerator import AnsatzGenerator
from ansatz_generator import AnsatzGenerator
import ast
import xacc
......
from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provides, \
Validate, Invalidate, Instantiate
from ansatzgenerator import AnsatzGenerator
from ansatz_generator import AnsatzGenerator
import ast
import xacc
......
from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provides, \
Validate, Invalidate, Instantiate
from ansatzgenerator import AnsatzGenerator
from ansatz_generator import AnsatzGenerator
import ast
import xacc
......
from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provides, \
Validate, Invalidate, Instantiate
from ansatzgenerator import AnsatzGenerator
from ansatz_generator import AnsatzGenerator
import ast
import xacc
......
from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provides, \
Validate, Invalidate, Instantiate
from ansatzgenerator import AnsatzGenerator
from ansatz_generator import AnsatzGenerator
import ast
import xacc
......
from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provides, \
Validate, Invalidate, Instantiate
from ansatzgenerator import AnsatzGenerator
from ansatz_generator import AnsatzGenerator
import ast
import xacc
......
from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provides, \
Validate, Invalidate, Instantiate
from ansatzgenerator import AnsatzGenerator
from ansatz_generator import AnsatzGenerator
import ast
import xacc
......
......@@ -3,7 +3,6 @@ from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provide
from hamiltonian_generator import HamiltonianGenerator
import ast
import xaccvqe
import numpy as np
try:
......
......@@ -2,8 +2,7 @@ from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provide
Validate, Invalidate, Instantiate
from hamiltonian_generator import HamiltonianGenerator
import ast
from xaccvqe import PauliOperator
from xacc import PauliOperator
@ComponentFactory("simpleH2_hamiltonian_generator_factory")
@Provides("hamiltonian_generator")
......
......@@ -2,8 +2,7 @@ from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provide
Validate, Invalidate, Instantiate
from hamiltonian_generator import HamiltonianGenerator
import ast, xacc
import xaccvqe as vqe
from xaccvqe import PauliOperator
from xacc import PauliOperator
@ComponentFactory("kh_4q_sto3g_hamiltonian_generator_factory")
......
......@@ -2,8 +2,7 @@ from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provide
Validate, Invalidate, Instantiate
from hamiltonian_generator import HamiltonianGenerator
import ast, xacc
import xaccvqe as vqe
from xaccvqe import PauliOperator
from xacc import PauliOperator
@ComponentFactory("nah_10qubit_hamiltonian_generator_factory")
......
......@@ -2,9 +2,7 @@ from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provide
Validate, Invalidate, Instantiate
from hamiltonian_generator import HamiltonianGenerator
import ast, xacc
import xaccvqe as vqe
from xaccvqe import PauliOperator
from xacc import PauliOperator
@ComponentFactory("nah_4q_sto3g_hamiltonian_generator_factory")
@Provides("hamiltonian_generator")
......@@ -14,88 +12,87 @@ from xaccvqe import PauliOperator
class NaH(HamiltonianGenerator):
def generate(self, inputParams):
src = """__qpu__ openfermion_kernel() {
-159.505
-0.714932 0 1 0 0
0.091683 0 1 1 0
0.091683 1 1 0 0
0.173443 1 1 1 0
-0.714932 2 1 2 0
0.091683 2 1 3 0
0.091683 3 1 2 0
0.173443 3 1 3 0
-0.0816923 0 1 1 1 0 0 1 0
0.0816923 0 1 1 1 1 0 0 0
-0.158901 0 1 2 1 0 0 2 0
0.0229208 0 1 2 1 0 0 3 0
0.0229208 0 1 2 1 1 0 2 0
-0.0202421 0 1 2 1 1 0 3 0
0.158901 0 1 2 1 2 0 0 0
-0.0229208 0 1 2 1 2 0 1 0
-0.0229208 0 1 2 1 3 0 0 0
0.0202421 0 1 2 1 3 0 1 0
0.0229208 0 1 3 1 0 0 2 0
-0.101934 0 1 3 1 0 0 3 0
-0.0202421 0 1 3 1 1 0 2 0
-0.00944179 0 1 3 1 1 0 3 0
-0.0229208 0 1 3 1 2 0 0 0
0.0202421 0 1 3 1 2 0 1 0
0.101934 0 1 3 1 3 0 0 0
0.00944179 0 1 3 1 3 0 1 0
0.0816923 1 1 0 1 0 0 1 0
-0.0816923 1 1 0 1 1 0 0 0
0.0229208 1 1 2 1 0 0 2 0
-0.0202421 1 1 2 1 0 0 3 0
-0.101934 1 1 2 1 1 0 2 0
-0.00944179 1 1 2 1 1 0 3 0
-0.0229208 1 1 2 1 2 0 0 0
0.101934 1 1 2 1 2 0 1 0
0.0202421 1 1 2 1 3 0 0 0
0.00944179 1 1 2 1 3 0 1 0
-0.0202421 1 1 3 1 0 0 2 0
-0.00944179 1 1 3 1 0 0 3 0
-0.00944179 1 1 3 1 1 0 2 0
-0.11747 1 1 3 1 1 0 3 0
0.0202421 1 1 3 1 2 0 0 0
0.00944179 1 1 3 1 2 0 1 0
0.00944179 1 1 3 1 3 0 0 0
0.11747 1 1 3 1 3 0 1 0
0.158901 2 1 0 1 0 0 2 0
-0.0229208 2 1 0 1 0 0 3 0
-0.0229208 2 1 0 1 1 0 2 0
0.0202421 2 1 0 1 1 0 3 0
-0.158901 2 1 0 1 2 0 0 0
0.0229208 2 1 0 1 2 0 1 0
0.0229208 2 1 0 1 3 0 0 0
-0.0202421 2 1 0 1 3 0 1 0
-0.0229208 2 1 1 1 0 0 2 0
0.0202421 2 1 1 1 0 0 3 0
0.101934 2 1 1 1 1 0 2 0
0.00944179 2 1 1 1 1 0 3 0
0.0229208 2 1 1 1 2 0 0 0
-0.101934 2 1 1 1 2 0 1 0
-0.0202421 2 1 1 1 3 0 0 0
-0.00944179 2 1 1 1 3 0 1 0
-0.0816923 2 1 3 1 2 0 3 0
0.0816923 2 1 3 1 3 0 2 0
-0.0229208 3 1 0 1 0 0 2 0
0.101934 3 1 0 1 0 0 3 0
0.0202421 3 1 0 1 1 0 2 0
0.00944179 3 1 0 1 1 0 3 0
0.0229208 3 1 0 1 2 0 0 0
-0.0202421 3 1 0 1 2 0 1 0
-0.101934 3 1 0 1 3 0 0 0
-0.00944179 3 1 0 1 3 0 1 0
0.0202421 3 1 1 1 0 0 2 0
0.00944179 3 1 1 1 0 0 3 0
0.00944179 3 1 1 1 1 0 2 0
0.11747 3 1 1 1 1 0 3 0
-0.0202421 3 1 1 1 2 0 0 0
-0.00944179 3 1 1 1 2 0 1 0
-0.00944179 3 1 1 1 3 0 0 0
-0.11747 3 1 1 1 3 0 1 0
0.0816923 3 1 2 1 2 0 3 0
-0.0816923 3 1 2 1 3 0 2 0
}"""
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 vqe.compile(src)
\ No newline at end of file
return xacc.getObservable("fermion", src)
\ No newline at end of file
......@@ -2,7 +2,6 @@ from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provide
Validate, Invalidate, Instantiate
from hamiltonian_generator import HamiltonianGenerator
import ast, xacc
import xaccvqe
try:
......
......@@ -23,13 +23,12 @@ class VQE(VQEBase):
"""
super().execute(inputParams)
# self.vqe_options_dict['task'] = 'vqe'
# if self.optimizer is not None:
# self.optimizer.optimize(self.op, self.buffer, self.optimizer_options, self.vqe_options_dict)
# else:
execParams = {'accelerator': self.qpu, 'ansatz': self.ansatz, 'observable': self.op, 'optimizer':self.optimizer}
vqe = xacc.getAlgorithm('vqe', execParams)
vqe.execute(self.buffer)
if 'optimizer' not in self.vqe_options_dict:
self.optimizer.optimize(self.buffer, self.optimizer_options, self.vqe_options_dict)
else:
vqe = xacc.getAlgorithm('vqe', self.vqe_options_dict)
vqe.execute(self.buffer)
return self.buffer
......
......@@ -99,28 +99,51 @@ class VQEBase(BenchmarkAlgorithm):
n_qubits = xaccOp.nBits()
self.op = xaccOp
self.n_qubits = n_qubits
# create buffer, add some extra info (hamiltonian, ansatz-qasm, python-ansatz-qasm)
self.buffer = self.qpu.createBuffer('q', n_qubits)
self.buffer.addExtraInfo('hamiltonian', str(xaccOp))
self.buffer.addExtraInfo('hamiltonian', self.op.toString())
self.buffer.addExtraInfo('ansatz-qasm', self.ansatz.toString('q').replace('\\n', '\\\\n'))
pycompiler = xacc.getCompiler('xacc-py')
self.buffer.addExtraInfo('ansatz-qasm-py', '\n'.join(pycompiler.translate('q',self.ansatz).split('\n')[1:]))
# heres where we can set up the algorithm Parameters
# all versions of vqe require: Accelerator, Ansatz, Observable
# pure-vqe requires Optimizer
# energy calculation has optional Parameters - can be random
self.vqe_options_dict = {'accelerator': self.qpu, 'ansatz': self.ansatz, 'observable': self.op}
# get optimizers for VQE
# needs to check if optimizer is a python plugin
# if not, use nlopt (with options)
# so we pull 'optimizer-options' out if available
# Optimizer-options needs to be passed to BOTH XACC Core optimizers and to python plugins
# vqe_options_dict is used to initialize the algorithms
self.optimizer = None
self.optimizer_options = {}
if 'optimizer' in inputParams and inputParams['optimizer'] in self.vqe_optimizers:
self.optimizer = self.vqe_optimizers[inputParams['optimizer']]
if 'method' in inputParams:
self.optimizer_options['method'] = inputParams['method']
if 'options' in inputParams:
self.optimizer_options['options'] = ast.literal_eval(inputParams['options'])
if 'user-params' in inputParams:
self.optimizer_options['options']['user_params'] = ast.literal_eval(inputParams['user-params'])
if 'optimizer-options' in inputParams:
self.optimizer_options = ast.literal_eval(inputParams['optimizer-options'])
# initial-parameters for optimizer (vqe)
# parameters for vqe-energy
if 'initial-parameters' in inputParams:
self.optimizer_options['initial-parameters'] = ast.literal_eval(inputParams['initial-parameters'])
if 'parameters' in inputParams:
self.optimizer_options['parameters'] = ast.literal_eval(inputParams['parameters'])
# check to see if optimizer is a python plugin
# if it is, we do not put it in self.vqe_options_dict
# if it is not, it is put there
if 'optimizer' in inputParams:
if inputParams['optimizer'] in self.vqe_optimizers:
self.optimizer = self.vqe_optimizers[inputParams['optimizer']]
else:
self.vqe_options_dict['optimizer'] = xacc.getOptimizer(inputParams['optimizer'], self.optimizer_options)
else:
xacc.info("No classical optimizer specified. Setting to default XACC optimizer.")
if 'options' in inputParams:
self.optimizer_options['options'] = ast.literal_eval(inputParams['options'])
self.optimizer = xacc.getOptimizer('nlopt', self.optimizer_options)
self.vqe_options_dict['optimizer'] = xacc.getOptimizer('nlopt', self.optimizer_options)
# vqe.py then will check vqe_options_dict for optimizer; if it isn't there, run python optimizer
# and of course if it is, we run with XACC
self.buffer.addExtraInfo('accelerator', inputParams['accelerator'])
# need to make sure the AcceleratorDecorators work correctly
if 'n-execs' in inputParams:
xacc.setOption('sampler-n-execs', inputParams['n-execs'])
self.qpu = xacc.getAcceleratorDecorator('improved-sampling', self.qpu)
......@@ -136,11 +159,6 @@ class VQEBase(BenchmarkAlgorithm):
if 'rdm-purification' in inputParams and inputParams['rdm-purification']:
self.qpu = xacc.getAcceleratorDecorator('rdm-purification', self.qpu)
self.vqe_options_dict = {'accelerator': self.qpu, 'ansatz': self.ansatz}
if 'initial-parameters' in inputParams:
self.vqe_options_dict['vqe-params'] = ','.join([str(x) for x in ast.literal_eval(inputParams['initial-parameters'])])
xacc.setOptions(inputParams)
def analyze(self, buffer, inputParams):
......
......@@ -27,14 +27,15 @@ class VQEEnergy(VQEBase):
- sets XACC VQE task to 'compute-energy'
"""
super().execute(inputParams)
self.vqe_options_dict['task'] = 'compute-energy'
energy = xaccvqe.execute(self.op, self.buffer, **self.vqe_options_dict).energy
if 'rdm-purification' in self.qpu.name():
p = self.buffer.getAllUnique('parameters')
child = self.buffer.getChildren('parameters', p[0])
energy = child[1].getInformation('purified-energy')
self.buffer.addExtraInfo('vqe-energy', energy)
algo = xacc.getAlgorithm("vqe-energy", self.vqe_options_dict)
algo.execute(self.buffer)
# if 'rdm-purification' in self.qpu.name():
# p = self.buffer.getAllUnique('parameters')
# child = self.buffer.getChildren('parameters', p[0])
# energy = child[1].getInformation('purified-energy')
# self.buffer.addExtraInfo('vqe-energy', energy)
return self.buffer
......
......@@ -6,7 +6,7 @@ from xacc import PauliOperator
class TestPyVQE(test.TestCase):
def setUp(self):
self.qpu = xacc.getAccelerator('tnqvm')
self.qpu = xacc.getAccelerator('local-ibm')
self.buffer = self.qpu.createBuffer('q', 2)
self.ham = PauliOperator(5.906709445) + \
PauliOperator({0: 'X', 1: 'X'}, -2.1433) + \
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment