Commit 10db0cc7 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding example showing readout error correction with aer accelerator


Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 5f3dd588
import xacc, numpy as np
from xacc import PauliOperator
nAngles = 10
# Get the local-ibm and tnqvm accelerators
# and allocate some qubits for execution on each
qpu = xacc.getAccelerator('aer', {'shots':1024, 'backend':'ibmq_johannesburg', 'readout_error':True})
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 = 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)
# 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)]
# ansatz(xacc.qalloc(2), 0.0)
# 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(5.906709445+re)
raw_energies.append(5.906709445+ra)
for c in tChildren:
tn += c.getInformation('coefficient')*c.getInformation('exp-val-z')
tnqvm_energies.append(5.906709445+tn)
for i,t in enumerate(np.linspace(-np.pi,np.pi,nAngles)):
print(t, tnqvm_energies[i], raw_energies[i], ro_energies[i])
......@@ -62,6 +62,10 @@ class AerAccelerator(xacc.Accelerator):
qobj = QasmQobj(qobj_id=qobj_json['qObject']['qobj_id'],
header=QobjHeader(), config=QasmQobjConfig(shots=self.shots, memory_slots=qobj_json['qObject']['config']['memory_slots']), experiments=exps, shots=self.shots)
measures = {}
for i in exps[0].instructions:
if i.name == "measure":
measures[i.memory[0]] =i.qubits[0]
backend = Aer.get_backend('qasm_simulator')
......@@ -71,7 +75,16 @@ class AerAccelerator(xacc.Accelerator):
job_sim = backend.run(qobj)
sim_result = job_sim.result()
[buffer.appendMeasurement(b,c) for b,c in sim_result.get_counts().items()]
for b,c in sim_result.get_counts().items():
bitstring = b
if len(b) < buffer.size():
tmp = ['0' for i in range(buffer.size())]
for bit_loc, qubit in measures.items():
tmp[len(tmp)-1-qubit] = list(b)[bit_loc]
bitstring = ''.join(tmp)
buffer.appendMeasurement(bitstring,c)
def execute(self, buffer, programs):
......@@ -79,8 +92,9 @@ class AerAccelerator(xacc.Accelerator):
if isinstance(programs, list) and len(programs) > 1:
for p in programs:
tmpBuffer = xacc.qalloc(buffer.size())
tmpBuffer.setName(p.name())
self.execute_one_qasm(tmpBuffer, p)
buffer.appendChild('',tmpBuffer)
buffer.appendChild(p.name(),tmpBuffer)
else:
if isinstance(programs, list):
programs = programs[0]
......
......@@ -477,6 +477,7 @@ PYBIND11_MODULE(_pyxacc, m) {
.def("resetBuffer", &xacc::AcceleratorBuffer::resetBuffer,
"Reset this buffer for use in another computation.")
.def("size", &xacc::AcceleratorBuffer::size, "")
.def("setName", &xacc::AcceleratorBuffer::setName, "")
.def("appendMeasurement",
(void (xacc::AcceleratorBuffer::*)(const std::string &)) &
xacc::AcceleratorBuffer::appendMeasurement,
......
......@@ -499,7 +499,7 @@ const std::string IBMAccelerator::processInput(
auto jsonStr = jj.dump();
std::cout << "JSON:\n" << jsonStr << "\n";
// exit(0);
exit(0);
return jsonStr;
}
}
......
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