Commit e6f09d15 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

removing vqe-energy, updated vqe with exec at params, minor doc fixes


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 1d801d5a
Pipeline #80606 passed with stage
in 3 minutes and 57 seconds
......@@ -28,7 +28,7 @@ execution metadata. Below demonstrate some basic usage of the ``AcceleratorBuffe
auto qpu = xacc::getAccelerator("ibm:ibmq_valencia");
qpu->execute(buffer, program);
std::map<std::string, int> results = buffer->getMeasurementCounts();
auto fidelities = buffer["1q-gate-fidelities"].as<std::vector<double>>();
auto fidelities = (*buffer)["1q-gate-fidelities"].as<std::vector<double>>();
auto expValZ = buffer->getExpectationValueZ();
in Python
......
......@@ -104,7 +104,7 @@ You can run Python examples as well
.. code:: bash
[ you may also want to add this to your .bashrc ]
$ export PYTHONPATH:$PYTHONPATH:$HOME/.xacc
$ export PYTHONPATH=$PYTHONPATH:$HOME/.xacc
$ python3 ../python/examples/ddcl_example.py
Most users build and install the TNQVM Accelerator
......
......@@ -188,7 +188,7 @@ execution metadata. Below demonstrate some basic usage of the <code class="docut
<span class="k">auto</span> <span class="n">qpu</span> <span class="o">=</span> <span class="n">xacc</span><span class="o">::</span><span class="n">getAccelerator</span><span class="p">(</span><span class="s">&quot;ibm:ibmq_valencia&quot;</span><span class="p">);</span>
<span class="n">qpu</span><span class="o">-&gt;</span><span class="n">execute</span><span class="p">(</span><span class="n">buffer</span><span class="p">,</span> <span class="n">program</span><span class="p">);</span>
<span class="n">std</span><span class="o">::</span><span class="n">map</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">string</span><span class="p">,</span> <span class="kt">int</span><span class="o">&gt;</span> <span class="n">results</span> <span class="o">=</span> <span class="n">buffer</span><span class="o">-&gt;</span><span class="n">getMeasurementCounts</span><span class="p">();</span>
<span class="k">auto</span> <span class="n">fidelities</span> <span class="o">=</span> <span class="n">buffer</span><span class="p">[</span><span class="s">&quot;1q-gate-fidelities&quot;</span><span class="p">].</span><span class="n">as</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">double</span><span class="o">&gt;&gt;</span><span class="p">();</span>
<span class="k">auto</span> <span class="n">fidelities</span> <span class="o">=</span> <span class="p">(</span><span class="o">*</span><span class="n">buffer</span><span class="p">)[</span><span class="s">&quot;1q-gate-fidelities&quot;</span><span class="p">].</span><span class="n">as</span><span class="o">&lt;</span><span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">double</span><span class="o">&gt;&gt;</span><span class="p">();</span>
<span class="k">auto</span> <span class="n">expValZ</span> <span class="o">=</span> <span class="n">buffer</span><span class="o">-&gt;</span><span class="n">getExpectationValueZ</span><span class="p">();</span>
</pre></div>
</div>
......
......@@ -259,7 +259,7 @@ $ quantum/examples/base_api/bell_quil_ibm_local
</div>
<p>You can run Python examples as well</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span><span class="o">[</span> you may also want to add this to your .bashrc <span class="o">]</span>
$ <span class="nb">export</span> PYTHONPATH:<span class="nv">$PYTHONPATH</span>:<span class="nv">$HOME</span>/.xacc
$ <span class="nb">export</span> <span class="nv">PYTHONPATH</span><span class="o">=</span><span class="nv">$PYTHONPATH</span>:<span class="nv">$HOME</span>/.xacc
$ python3 ../python/examples/ddcl_example.py
</pre></div>
</div>
......
......@@ -28,7 +28,7 @@ execution metadata. Below demonstrate some basic usage of the ``AcceleratorBuffe
auto qpu = xacc::getAccelerator("ibm:ibmq_valencia");
qpu->execute(buffer, program);
std::map<std::string, int> results = buffer->getMeasurementCounts();
auto fidelities = buffer["1q-gate-fidelities"].as<std::vector<double>>();
auto fidelities = (*buffer)["1q-gate-fidelities"].as<std::vector<double>>();
auto expValZ = buffer->getExpectationValueZ();
in Python
......
......@@ -104,7 +104,7 @@ You can run Python examples as well
.. code:: bash
[ you may also want to add this to your .bashrc ]
$ export PYTHONPATH:$PYTHONPATH:$HOME/.xacc
$ export PYTHONPATH=$PYTHONPATH:$HOME/.xacc
$ python3 ../python/examples/ddcl_example.py
Most users build and install the TNQVM Accelerator
......
......@@ -43,13 +43,6 @@ vqe = xacc.getAlgorithm('vqe', {
})
vqe.execute(buffer)
# Now lets just do a param sweep
ansatz.modifyAlgorithm('energy')
# Execute, starting at .5
def compute_energy_at_angles(angles):
ansatz(buffer, *angles)
return buffer.getInformation('opt-val')
energy = lambda angles : compute_energy_at_angles(angles)
energy = lambda angles : vqe.execute(buffer, angles)[0]
print([energy([a]) for a in np.linspace(-np.pi,np.pi,50)])
......@@ -45,14 +45,15 @@ for p in uniqueParams:
re, ra, tn = [0.0 for i in range(3)]
# print(children[0])
for c in children:
print(c.name(), c.getInformation('kernel'))
coeff = c.getInformation('coefficient')
re += coeff * c.getInformation('ro-fixed-exp-val-z')
ra += coeff * c.getInformation('exp-val-z')
ro_energies.append(5.906709445+re)
raw_energies.append(5.906709445+ra)
ro_energies.append(re)
raw_energies.append(ra)
for c in tChildren:
tn += c.getInformation('coefficient')*c.getInformation('exp-val-z')
tnqvm_energies.append(5.906709445+tn)
tnqvm_energies.append(tn)
for i,t in enumerate(np.linspace(-np.pi,np.pi,nAngles)):
print(t, tnqvm_energies[i], raw_energies[i], ro_energies[i])
import xacc, numpy as np
nAngles = 10
# Get the local-ibm and tnqvm accelerators
# and allocate some qubits for execution on each
qpu = xacc.getAccelerator('local-ibm', {'shots':8192, 'global-p10':.1,'global-p01':.01})
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
# accelerator with TNQVM
print("Starting TNQVM")
ansatz.overrideAccelerator(tnqvm)
[ansatz(tnqvmBuffer, t) for t in np.linspace(-np.pi,np.pi,nAngles)]
# Get Theory, Raw, and ROError energies in separate lists
ro_energies, raw_energies, tnqvm_energies = [[] for i in range(3)]
uniqueParams = buffer.getAllUnique('parameters')
for p in uniqueParams:
children = buffer.getChildren('parameters', p)
tChildren = tnqvmBuffer.getChildren('parameters',p)
re, ra, tn = [0.0 for i in range(3)]
# print(children[0])
for c in children:
coeff = c.getInformation('coefficient')
re += coeff * c.getInformation('ro-fixed-exp-val-z')
ra += coeff * c.getInformation('exp-val-z')
ro_energies.append(re)
raw_energies.append(ra)
for c in tChildren:
tn += c.getInformation('coefficient')*c.getInformation('exp-val-z')
tnqvm_energies.append(tn)
for i,t in enumerate(np.linspace(-np.pi,np.pi,nAngles)):
print(t, tnqvm_energies[i], raw_energies[i], ro_energies[i])
......@@ -24,6 +24,6 @@ qpu = xacc.getAccelerator('tnqvm')
def ansatz_vqe(q, t0, t1):
uccsd(q,{"ne":2,"nq":4})
print(ansatz_vqe.getFunction().toString())
print(ansatz_vqe.getCompositeInstruction().toString())
buffer = xacc.qalloc(4)
ansatz_vqe(buffer, 0.,0.)
\ No newline at end of file
......@@ -224,7 +224,7 @@ class DecoratorFunction(ABC):
def nParameters(self):
return self.getFunction().nParameters()
def getFunction(self):
def getCompositeInstruction(self):
return self.compiledKernel
def modifyAlgorithm(self, algorithm):
......
......@@ -12,9 +12,7 @@
# *******************************************************************************/
add_subdirectory(vqe)
add_subdirectory(rdm)
add_subdirectory(vqe-energy)
add_subdirectory(ml)
file(GLOB PYDECORATORS ${CMAKE_CURRENT_SOURCE_DIR}/vqe/python/*.py
${CMAKE_CURRENT_SOURCE_DIR}/vqe-energy/python/*.py
${CMAKE_CURRENT_SOURCE_DIR}/ml/ddcl/python/*.py)
install(FILES ${PYDECORATORS} DESTINATION ${CMAKE_INSTALL_PREFIX}/py-plugins)
......@@ -78,7 +78,7 @@ TEST(DDCLTester, checkSimpleWithGradient) {
<< "\n";
auto loss = buffer->getInformation("opt-val").as<double>();
EXPECT_NEAR(loss, 0.0, 1e-4);
EXPECT_NEAR(loss, 0.0, 1e-3);
}
}
......
# *******************************************************************************
# Copyright (c) 2019 UT-Battelle, LLC.
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# and Eclipse Distribution License v.10 which accompany this distribution.
# The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
# and the Eclipse Distribution License is available at
# https://eclipse.org/org/documents/edl-v10.php
#
# Contributors:
# Alexander J. McCaskey - initial API and implementation
# *******************************************************************************/
set(LIBRARY_NAME xacc-algorithm-vqe-energy)
file(GLOB SRC *.cpp)
usfunctiongetresourcesource(TARGET ${LIBRARY_NAME} OUT SRC)
usfunctiongeneratebundleinit(TARGET ${LIBRARY_NAME} OUT SRC)
add_library(${LIBRARY_NAME} SHARED ${SRC})
target_include_directories(
${LIBRARY_NAME}
PUBLIC .)
target_link_libraries(${LIBRARY_NAME} PUBLIC xacc CppMicroServices)
set(_bundle_name xacc_algorithm_vqe_energy)
set_target_properties(${LIBRARY_NAME}
PROPERTIES COMPILE_DEFINITIONS
US_BUNDLE_NAME=${_bundle_name}
US_BUNDLE_NAME
${_bundle_name})
usfunctionembedresources(TARGET
${LIBRARY_NAME}
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
FILES
manifest.json)
if(APPLE)
set_target_properties(${LIBRARY_NAME}
PROPERTIES INSTALL_RPATH "@loader_path/../lib")
set_target_properties(${LIBRARY_NAME}
PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
else()
set_target_properties(${LIBRARY_NAME}
PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")
set_target_properties(${LIBRARY_NAME} PROPERTIES LINK_FLAGS "-shared")
endif()
if(XACC_BUILD_TESTS)
add_subdirectory(tests)
endif()
install(TARGETS ${LIBRARY_NAME} DESTINATION ${CMAKE_INSTALL_PREFIX}/plugins)
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