Skip to content

Commit

Permalink
Convert IonQ outputs to bit strings and correct order (NVIDIA#575)
Browse files Browse the repository at this point in the history
This will enable more nightly integration tests to be enabled and pass.
  • Loading branch information
bmhowe23 authored Aug 23, 2023
1 parent c216202 commit f440d60
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "common/RestClient.h"
#include "common/ServerHelper.h"
#include "cudaq/utils/cudaq_utils.h"
#include <bitset>
#include <fstream>
#include <thread>

Expand Down Expand Up @@ -248,14 +249,33 @@ IonQServerHelper::processResults(ServerMessage &postJobResponse) {
auto resultsGetPath = constructGetResultsPath(postJobResponse);
// Get the results
auto results = getResults(resultsGetPath);

// Get the number of qubits. This assumes the all qubits are measured, which
// is a safe assumption for now but may change in the future.
cudaq::debug("postJobResponse message: {}", postJobResponse.dump());
auto &jobs = postJobResponse.at("jobs");
if (!jobs[0].contains("qubits"))
throw std::runtime_error(
"ServerMessage doesn't tell us how many qubits there were");

auto nQubits = jobs[0].at("qubits").get<int>();
cudaq::debug("nQubits is : {}", nQubits);
cudaq::debug("Results message: {}", results.dump());

cudaq::CountsDictionary counts;

// Process the results
assert(nQubits <= 64);
for (const auto &element : results.items()) {
std::string key = element.key();
// Convert base-10 ASCII key to bitstring and perform endian swap
uint64_t s = std::stoull(element.key());
std::string newkey = std::bitset<64>(s).to_string();
std::reverse(newkey.begin(), newkey.end()); // perform endian swap
newkey.resize(nQubits);

double value = element.value().get<double>();
std::size_t count = static_cast<std::size_t>(value * shots);
counts[key] = count;
counts[newkey] = count;
}

// Create an execution result
Expand Down
8 changes: 6 additions & 2 deletions utils/mock_qpu/ionq/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class Job(BaseModel):
# Could how many times the client has requested the Job
countJobGetRequests = 0

# Save how many qubits were needed for each test (emulates real backend)
numQubitsRequired = 0

llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmprinter()
Expand Down Expand Up @@ -76,7 +79,7 @@ async def login(token: Union[str, None] = Header(alias="Authorization",
async def postJob(job: Job,
token: Union[str, None] = Header(alias="Authorization",
default=None)):
global createdJobs, shots
global createdJobs, shots, numQubitsRequired

if token == None:
raise HTTPException(status_code(401), detail="Credentials not provided")
Expand Down Expand Up @@ -125,7 +128,7 @@ async def postJob(job: Job,
# until we return the job results
@app.get("/v0.3/jobs")
async def getJob(id: str):
global countJobGetRequests, createdJobs
global countJobGetRequests, createdJobs, numQubitsRequired

# Simulate asynchronous execution
if countJobGetRequests < 3:
Expand All @@ -136,6 +139,7 @@ async def getJob(id: str):
res = {
"jobs": [{
"status": "completed",
"qubits": numQubitsRequired,
"results_url": "/v0.3/jobs/{}/results".format(id)
}]
}
Expand Down

0 comments on commit f440d60

Please sign in to comment.