Skip to content

Commit

Permalink
Merge pull request #39 from danclaudino/ionq_noisy_sim
Browse files Browse the repository at this point in the history
Enabled IonQ noisy simulation
  • Loading branch information
wongey authored Aug 21, 2024
2 parents c7dc85d + 6c5733c commit 61dc583
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
36 changes: 28 additions & 8 deletions quantum/plugins/ionq/ionq_accelerator.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* Copyright (c) 2024 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
Expand All @@ -9,6 +9,7 @@
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
* Daniel Claudino - Enabled access to noisy simulation
*******************************************************************************/
#include "ionq_accelerator.hpp"
#include <cctype>
Expand All @@ -33,6 +34,21 @@ HeterogeneousMap IonQAccelerator::getProperties() {
return m;
}

void IonQAccelerator::updateConfiguration(const HeterogeneousMap &config) {
if (config.keyExists<int>("shots")) {
shots = config.get<int>("shots");
}
if (config.stringExists("backend")) {
backend = config.getString("backend");
if (backend.find("simulator") != std::string::npos ||
backend.find("sim") != std::string::npos &&
backend.find(".") != std::string::npos) {
noise_model = backend.substr(backend.find(".") + 1);
backend = "simulator";
}
}
}

void IonQAccelerator::initialize(const HeterogeneousMap &params) {
if (!initialized) {
updateConfiguration(params);
Expand All @@ -46,14 +62,15 @@ void IonQAccelerator::initialize(const HeterogeneousMap &params) {
auto characterizations = restClient->get(url, "/jobs", headers);
auto j = nlohmann::json::parse(characterizations);
// std::cout << j.dump(1) << std::endl;
// m_connectivity = j["characterizations"][0]["connectivity"].get<std::vector<std::pair<int,int>>>();

// m_connectivity =
// j["characterizations"][0]["connectivity"].get<std::vector<std::pair<int,int>>>();

remoteUrl = url;
postPath = "/jobs";
}
}

// Note: IonQ don't support batching.
// Note: IonQ don't support batching.
void IonQAccelerator::execute(
std::shared_ptr<AcceleratorBuffer> buffer,
const std::vector<std::shared_ptr<CompositeInstruction>> circuits) {
Expand All @@ -62,7 +79,6 @@ void IonQAccelerator::execute(
std::make_shared<xacc::AcceleratorBuffer>(f->name(), buffer->size());
// Run each circuit
RemoteAccelerator::execute(tmpBuffer, f);
// tmpBuffer->print();
buffer->appendChild(f->name(), tmpBuffer);
}
}
Expand All @@ -74,6 +90,9 @@ const std::string IonQAccelerator::processInput(
xacc::ionq::IonQProgram prog;
prog.set_shots(shots);
prog.set_target(backend);
if (!noise_model.empty()) {
prog.set_noise_model(noise_model);
}

auto visitor = std::make_shared<IonQProgramVisitor>();

Expand Down Expand Up @@ -145,9 +164,11 @@ void IonQAccelerator::processResponse(std::shared_ptr<AcceleratorBuffer> buffer,
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
// End the color log
std::cout << "\033[0m" << "\n";
std::cout << "\033[0m"
<< "\n";

auto results = handleExceptionRestClientGet(url, "/jobs/" + jobId + "/results", headers);
auto results =
handleExceptionRestClientGet(url, "/jobs/" + jobId + "/results", headers);
std::map<std::string, double> histogram = json::parse(results);

int n = buffer->size();
Expand Down Expand Up @@ -220,4 +241,3 @@ void IonQAccelerator::findApiKeyInFile(std::string &apiKey, std::string &url,
}
} // namespace quantum
} // namespace xacc

13 changes: 4 additions & 9 deletions quantum/plugins/ionq/ionq_accelerator.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* Copyright (c) 2024 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
Expand All @@ -9,6 +9,7 @@
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
* Daniel Claudino - Enabled access to noisy simulation
*******************************************************************************/
#ifndef QUANTUM_GATE_ACCELERATORS_IONQACCELERATOR_HPP_
#define QUANTUM_GATE_ACCELERATORS_IONQACCELERATOR_HPP_
Expand All @@ -28,14 +29,7 @@ class IonQAccelerator : public RemoteAccelerator {
const std::vector<std::shared_ptr<CompositeInstruction>>
circuits) override;
void initialize(const HeterogeneousMap &params = {}) override;
void updateConfiguration(const HeterogeneousMap &config) override {
if (config.keyExists<int>("shots")) {
shots = config.get<int>("shots");
}
if (config.stringExists("backend")) {
backend = config.getString("backend");
}
}
void updateConfiguration(const HeterogeneousMap &config) override;

const std::vector<std::string> configurationKeys() override {
return {"shots", "backend"};
Expand Down Expand Up @@ -74,6 +68,7 @@ class IonQAccelerator : public RemoteAccelerator {

int shots = 1024;
std::string backend = "simulator";
std::string noise_model;
std::vector<std::pair<int,int>> m_connectivity;

bool jobIsRunning = false;
Expand Down
7 changes: 7 additions & 0 deletions quantum/plugins/ionq/json/ionq_program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ namespace ionq {
private:
std::string lang = "json";
std::string target;
std::string noise_model = "ideal";
std::int64_t shots;
Body body;

Expand All @@ -89,6 +90,10 @@ namespace ionq {
std::string & get_mutable_target() { return target; }
void set_target(const std::string & value) { this->target = value; }

const std::string & get_noise_model() const { return noise_model; }
std::string & get_mutable_noise_model() { return noise_model; }
void set_noise_model(const std::string & value) { this->noise_model = value; }

const std::int64_t & get_shots() const { return shots; }
std::int64_t & get_mutable_shots() { return shots; }
void set_shots(const std::int64_t & value) { this->shots = value; }
Expand Down Expand Up @@ -145,6 +150,7 @@ namespace nlohmann {
x.set_target(j.at("target").get<std::string>());
x.set_shots(j.at("shots").get<std::int64_t>());
x.set_body(j.at("input").get<xacc::ionq::Body>());
x.set_noise_model(j.at("noise").at("model").get<std::string>());
}

inline void to_json(json & j, const xacc::ionq::IonQProgram & x) {
Expand All @@ -153,6 +159,7 @@ namespace nlohmann {
j["target"] = x.get_target();
j["shots"] = x.get_shots();
j["input"] = x.get_body();
j["noise"] = { {"model", x.get_noise_model()} };
}
}

9 changes: 7 additions & 2 deletions quantum/plugins/ionq/tests/IonQProgramTester.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 UT-Battelle, LLC.
* Copyright (c) 2024 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
Expand All @@ -9,6 +9,7 @@
*
* Contributors:
* Alexander J. McCaskey - initial API and implementation
* Daniel Claudino - Enabled access to noisy simulation
*******************************************************************************/
#include "ionq_program.hpp"
#include <gtest/gtest.h>
Expand Down Expand Up @@ -37,14 +38,18 @@ TEST(IonQProgramTester, checkFromJson) {
"target": 1
}
]
},
"noise": {
"model": "ideal"
}
})json";
using json = nlohmann::json;
xacc::ionq::IonQProgram root;
auto j = json::parse(str);
from_json(j, root);

std::cout << "HELLO: " << root.get_target() << "\n";
std::cout << "HELLO: " << root.get_target() << "\nNoise model: " << root.get_noise_model() << "\n";


json jj;

Expand Down

0 comments on commit 61dc583

Please sign in to comment.