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

Fixing bug in getting string from heterogeneous map created in C++ and passed to Python


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent ecee1ae0
Pipeline #82121 passed with stage
in 4 minutes and 18 seconds
......@@ -14,7 +14,7 @@ symmetry c1
fo = [0, 1, 2, 3, 4, 10, 11, 12, 13, 14]
ao = [5, 9, 15, 19]
H = xacc.getObservable('psi4-frozen-core', {'basis': 'sto-3g', 'geometry': geom,
H = xacc.getObservable('psi4', {'basis': 'sto-3g', 'geometry': geom,
'frozen-spin-orbitals': fo, 'active-spin-orbitals': ao})
@xacc.qpu(algo='vqe', accelerator=qpu, observable=H)
......@@ -24,9 +24,6 @@ def ansatz(q, x):
ucc1(q, x[0])
print(ansatz.getFunction().toString())
print(ansatz.getFunction().getVariables())
ansatz(buffer, 0.0)
print('Energy = ', buffer.getInformation('opt-val'))
......
import xacc
# Get access to the desired QPU and
# allocate some qubits to run on
qpu = xacc.getAccelerator('tnqvm', {'vqe-mode': True})
buffer = xacc.qalloc(4)
geom = '''
0 1
H 0.000000 0.0 0.0
H 0.0 0.0 .7474
symmetry c1
'''
fo = [0, 1, 2, 3, 4, 10, 11, 12, 13, 14]
ao = [5, 9, 15, 19]
H = xacc.getObservable('psi4', {'basis': 'sto-3g', 'geometry': geom})#,
# 'frozen-spin-orbitals': fo, 'active-spin-orbitals': ao})
@xacc.qpu(algo='vqe', accelerator=qpu, observable=H)
def ansatz(q, x):
X(q[0])
X(q[2])
ucc1(q, x[0])
ansatz(buffer, 0.0)
print('Energy = ', buffer.getInformation('opt-val'))
print('Opt Angles = ', buffer.getInformation('opt-params'))
......@@ -4,10 +4,10 @@ from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provide
@ComponentFactory("fc_observable_factory")
@Provides("observable")
@Property("_observable", "observable", "psi4-frozen-core")
@Property("_name", "name", "psi4-frozen-core")
@Instantiate("psi4_frozen_core_accelerator_instance")
class Psi4FrozenCore(xacc.Observable):
@Property("_observable", "observable", "psi4")
@Property("_name", "name", "psi4")
@Instantiate("psi4_observable_instance")
class Psi4Observable(xacc.Observable):
def __init__(self):
xacc.Observable.__init__(self)
......@@ -23,7 +23,7 @@ class Psi4FrozenCore(xacc.Observable):
return self.observable.nBits()
def name(self):
return 'psi4-frozen-core'
return 'psi4'
def fromOptions(self, inputParams):
import numpy as np
......@@ -126,10 +126,17 @@ class Psi4FrozenCore(xacc.Observable):
[np.zeros_like(H_core_ao), H_core_ao]])
Hamiltonian_1body = np.einsum('ij, jk, kl -> il', C.T, Hamiltonian_1body_ao, C)
Hamiltonian_2body = gmo
MSO_frozen_list = inputParams['frozen-spin-orbitals']
MSO_active_list = inputParams['active-spin-orbitals']
n_frozen = len(MSO_frozen_list)
n_active = len(MSO_active_list)
if 'frozen-spin-orbitals' in inputParams and 'active-spin-orbitals' in inputParams:
MSO_frozen_list = inputParams['frozen-spin-orbitals']
MSO_active_list = inputParams['active-spin-orbitals']
n_frozen = len(MSO_frozen_list)
n_active = len(MSO_active_list)
else:
MSO_frozen_list = []
MSO_active_list = range(Hamiltonian_1body.shape[0])
n_frozen = 0
n_active = len(MSO_active_list)
# ----- 0-body frozen-core:
Hamiltonian_fc_0body = E_nucl
......
......@@ -52,8 +52,8 @@ void bind_heterogeneous_map(py::module &m) {
return m.get<bool>(key);
} else if (m.keyExists<double>(key)) {
return m.get<double>(key);
} else if (m.keyExists<std::string>(key)) {
return m.get<std::string>(key);
} else if (m.stringExists(key)) {
return m.getString(key);
} else if (m.keyExists<std::vector<double>>(key)) {
return m.get<std::vector<double>>(key);
} else if (m.keyExists<std::vector<int>>(key)) {
......@@ -74,7 +74,7 @@ void bind_heterogeneous_map(py::module &m) {
"__contains__",
[](HeterogeneousMap &m, const std::string key) {
return m.keyExists<int>(key) || m.keyExists<double>(key) ||
m.keyExists<bool>(key) || m.keyExists<std::string>(key) ||
m.keyExists<bool>(key) || m.stringExists(key) ||
m.keyExists<std::vector<double>>(key) ||
m.keyExists<std::vector<int>>(key) ||
m.keyExists<std::vector<std::string>>(key) ||
......
......@@ -20,4 +20,8 @@ add_executable(deuteronH3_from_qasm_vqe_algo deuteronH3_vqe_algo.cpp)
target_link_libraries(deuteronH3_from_qasm_vqe_algo PRIVATE xacc xacc-quantum-gate)
add_executable(ddcl_example ddcl_example.cpp)
target_link_libraries(ddcl_example PRIVATE xacc)
\ No newline at end of file
target_link_libraries(ddcl_example PRIVATE xacc)
add_executable(bell_noisy bell_readout_py_load.cpp)
target_link_libraries(bell_noisy PRIVATE xacc)
\ No newline at end of file
/*******************************************************************************
* 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 v1.0 which accompanies 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
*******************************************************************************/
#include "xacc.hpp"
int main(int argc, char **argv) {
xacc::Initialize(argc, argv);
xacc::external::load_external_language_plugins();
xacc::set_verbose(true);
auto accelerator = xacc::getAccelerator(
"aer", {std::make_pair("backend", "ibmq_johannesburg"),
std::make_pair("readout_error", true)});
xacc::qasm(R"(
.compiler xasm
.circuit bell
.qbit q
H(q[0]);
CX(q[0],q[1]);
Measure(q[0]);
Measure(q[1]);
)");
auto bell = xacc::getCompiled("bell");
// Allocate some qubits and execute
auto buffer = xacc::qalloc(2);
accelerator->execute(buffer, bell);
buffer->print();
xacc::external::unload_external_language_plugins();
xacc::Finalize();
}
\ No newline at end of file
......@@ -20,6 +20,7 @@
#include "OptionsProvider.hpp"
#include "Cloneable.hpp"
#include "Algorithm.hpp"
#include "Compiler.hpp"
#include "Observable.hpp"
#include "Optimizer.hpp"
......@@ -41,7 +42,7 @@ namespace xacc {
using ContributableService =
Variant<std::shared_ptr<Instruction>, std::shared_ptr<Accelerator>,
std::shared_ptr<Compiler>, std::shared_ptr<Optimizer>,
std::shared_ptr<Compiler>, std::shared_ptr<Optimizer>, std::shared_ptr<Algorithm>,
std::shared_ptr<IRTransformation>, std::shared_ptr<Observable>,
std::shared_ptr<AcceleratorDecorator>>;
......
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