Commit 60fd88a6 authored by Mccaskey, Alex's avatar Mccaskey, Alex
Browse files

adding extra options to compiler, set up aer to check for noise model file,...


adding extra options to compiler, set up aer to check for noise model file, adding first pass at mitiq decorator
Signed-off-by: Mccaskey, Alex's avatarAlex McCaskey <mccaskeyaj@ornl.gov>
parent 713b715b
......@@ -54,6 +54,7 @@ file(GLOB PYDECORATORS benchmark/chemistry/*.py benchmark/qpt/*.py
plugins/cirq/*.py
plugins/qiskit/*.py
plugins/observables/*.py
plugins/mitiq/*.py
plugins/pyzx/*.py
plugins/optimizers/*.py)
install(FILES ${PYDECORATORS} DESTINATION ${CMAKE_INSTALL_PREFIX}/py-plugins)
import xacc
noiseModelJson ='{"errors": [{"type": "qerror", "operations": ["u1"], "instructions": [[{"name": "x", "qubits": [0]}], [{"name": "y", "qubits": [0]}], [{"name": "z", "qubits": [0]}], [{"name": "id", "qubits": [0]}]], "probabilities": [0.00025, 0.00025, 0.00025, 0.99925]}, {"type": "qerror", "operations": ["u2"], "instructions": [[{"name": "x", "qubits": [0]}], [{"name": "y", "qubits": [0]}], [{"name": "z", "qubits": [0]}], [{"name": "id", "qubits": [0]}]], "probabilities": [0.00025, 0.00025, 0.00025, 0.99925]}, {"type": "qerror", "operations": ["u3"], "instructions": [[{"name": "x", "qubits": [0]}], [{"name": "y", "qubits": [0]}], [{"name": "z", "qubits": [0]}], [{"name": "id", "qubits": [0]}]], "probabilities": [0.00025, 0.00025, 0.00025, 0.99925]}], "x90_gates": []}'
qpu = xacc.getAccelerator('aer', {'noise-model':noiseModelJson, 'shots':4096})
qpu = xacc.getAcceleratorDecorator('mitiq', qpu)
q = xacc.qalloc(1)
xacc.qasm('''.compiler xasm
.circuit foo
.parameters x
.qbit q
for (int i = 0; i < 10; i++) {
X(q[0]);
}
Measure(q[0]);
''')
foo = xacc.getCompiled('foo')
qpu.execute(q, foo)
print(q)
print(q.getExpectationValueZ())
import xacc
from xacc import CompositeInstruction
from pelix.ipopo.decorators import ComponentFactory, Property, Requires, Provides, \
Validate, Invalidate, Instantiate
@ComponentFactory("mitiq_decorator_factory")
@Provides("accelerator_decorator")
@Property("_accelerator_decorator", "accelerator_decorator", "mitiq")
@Property("_name", "name", "mitiq")
@Instantiate("mitiq_accelerator_decorator_instance")
class MitiqDecorator(xacc.AcceleratorDecorator):
def __init__(self):
xacc.AcceleratorDecorator.__init__(self)
def initialize(self, options):
self.openqasm_compiler = xacc.getCompiler('staq')
self.openqasm_compiler.setExtraOptions({'no-optimize': True})
return
def name(self):
return 'mitiq'
def execute_single(self, buffer, program):
import mitiq
from qiskit import QuantumCircuit
def noisy_sim(circ : QuantumCircuit):
# Convert circ to a CompositeInstruction
out_src = circ.qasm()
# print('hello src:\n', out_src)
out_prog = self.openqasm_compiler.compile(out_src).getComposites()[0]
# print('after compile:\n', out_prog.toString())
# Execute on a tmp buffer
tmp_buffer = xacc.qalloc(buffer.size())
self.decoratedAccelerator.execute(tmp_buffer, out_prog)
# record the noisy exp val
buffer.addExtraInfo('noisy-exp-val-z', tmp_buffer.getExpectationValueZ())
buffer.appendChild('mitiq-noisy-exec-'+buffer.name(), tmp_buffer)
return tmp_buffer.getExpectationValueZ()
# easiest thing to do is map to Qiskit
src = self.openqasm_compiler.translate(program).replace('\\','')
# Create a QuantumCircuit
circuit = QuantumCircuit.from_qasm_str(src)
# print(circuit.qasm())
fixed_exp = mitiq.execute_with_zne(circuit, noisy_sim)
# print(fixed_exp)
buffer.addExtraInfo('exp-val-z', fixed_exp)
return
def execute(self, buffer, programs):
# Translate IR to a Qobj Json String
if isinstance(programs, list) and len(programs) > 1:
for p in programs:
tmpBuffer = xacc.qalloc(buffer.size())
tmpBuffer.setName(p.name())
self.execute_single(tmpBuffer, p)
buffer.appendChild(p.name(),tmpBuffer)
else:
if isinstance(programs, list):
programs = programs[0]
self.execute_single(buffer, programs)
......@@ -50,5 +50,16 @@ void bind_compiler(py::module &m) {
}
return c.translate(prog, m);
},
"")
.def(
"setExtraOptions",
[](xacc::Compiler &c, const PyHeterogeneousMap &opts) {
HeterogeneousMap m;
for (auto &item : opts) {
PyHeterogeneousMap2HeterogeneousMap vis(m, item.first);
mpark::visit(vis, item.second);
}
c.setExtraOptions(m);
},
"");
}
\ No newline at end of file
......@@ -100,7 +100,16 @@ void AerAccelerator::initialize(const HeterogeneousMap &params) {
// std::cout << "NoiseModelJson:\n" << noise_model.dump(4) << "\n";
} else if (params.stringExists("noise-model")) {
noise_model = nlohmann::json::parse(params.getString("noise-model"));
std::string noise_model_str = params.getString("noise-model");
// Check if this is a file name
std::ifstream test(noise_model_str);
if (test) {
std::string str((std::istreambuf_iterator<char>(test)),
std::istreambuf_iterator<char>());
noise_model = nlohmann::json::parse(str);
} else {
noise_model = nlohmann::json::parse(params.getString("noise-model"));
}
}
}
double AerAccelerator::calcExpectationValueZ(
......
......@@ -74,8 +74,8 @@ bool StaqCompiler::canParse(const std::string &src) {
for (auto &l : lines) {
xacc::trim(l);
if (l.find("measure") != std::string::npos) {
// don't add measures
continue;
// don't add measures
continue;
}
tmp += l + "\n";
if (l.find("include") != std::string::npos) {
......@@ -89,7 +89,7 @@ bool StaqCompiler::canParse(const std::string &src) {
_src = tmp;
if (!addedNames) {
for (auto &b : bufferNames) {
auto size = std::numeric_limits<int>::max();
auto size = std::numeric_limits<int>::max();
_src = "qreg " + b + "[" + std::to_string(size) + "];\n" + _src;
}
}
......@@ -116,7 +116,7 @@ bool StaqCompiler::canParse(const std::string &src) {
_src = "OPENQASM 2.0;\n" + _src;
}
// std::cout << " HELLO:\n" << _src << "\n";
// std::cout << " HELLO:\n" << _src << "\n";
using namespace staq;
try {
......@@ -196,7 +196,7 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
transformations::desugar(*prog);
transformations::synthesize_oracles(*prog);
optimization::simplify(*prog);
if (run_staq_optimize) optimization::simplify(*prog);
// at this point we have to find out if we have any ancilla
// registers
......@@ -217,7 +217,7 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
transformations::inline_ast(
*prog, {false, transformations::default_overrides, "anc"});
// std::cout <<"PROG: " << *prog << "\n";
// std::cout <<"PROG: " << *prog << "\n";
// Visit Program to find out how many qreg there are and
// use that to build up openqasm xacc function prototype
......@@ -263,7 +263,7 @@ std::shared_ptr<IR> StaqCompiler::compile(const std::string &src,
xx << ") {\n" << translate.ss.str() << "}";
kernel = xx.str();
}
// std::cout << "\n\nFinal:\n" << kernel << "\n";
// std::cout << "\n\nFinal:\n" << kernel << "\n";
return xasm->compile(kernel, acc);
}
......
......@@ -19,6 +19,8 @@ namespace xacc {
class StaqCompiler : public xacc::Compiler {
protected:
bool run_staq_optimize = true;
public:
StaqCompiler();
......@@ -26,7 +28,12 @@ public:
std::shared_ptr<Accelerator> acc) override;
std::shared_ptr<xacc::IR> compile(const std::string &src) override;
bool canParse(const std::string& src) override;
void setExtraOptions(const HeterogeneousMap options) override {
if (options.keyExists<bool>("no-optimize") && options.get<bool>("no-optimize")) {
run_staq_optimize = false;
}
}
bool canParse(const std::string &src) override;
const std::string
translate(std::shared_ptr<CompositeInstruction> function) override;
......
......@@ -27,6 +27,9 @@ public:
virtual std::shared_ptr<IR> compile(const std::string &src,
std::shared_ptr<Accelerator> acc) = 0;
virtual std::shared_ptr<IR> compile(const std::string &src) = 0;
virtual void setExtraOptions(const HeterogeneousMap options) {
return;
}
virtual const std::string get_statement_terminator() {
return ";";
......
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