Commit ccb676fa authored by Nguyen, Thien's avatar Nguyen, Thien

Added a set of teleport tests to confirm correctness

Signed-off-by: Nguyen, Thien's avatarThien Nguyen <nguyentm@ornl.gov>
parent ba5b8aa9
import xacc
qpu = xacc.getAccelerator('qpp', {'shots':100})
qpu = xacc.getAccelerator('aer', {'shots':1024})
xacc.qasm('''
.compiler xasm
......
......@@ -50,6 +50,7 @@ public:
qubit2MemorySlot.insert({b, counter});
counter++;
}
xacc::ibm::RegisterAllocator::getInstance()->reset();
}
const std::string toString() override {
......
......@@ -292,9 +292,9 @@ public:
struct Bfunc {
Bfunc(int64_t in_regId, const std::string &in_hexMask,
const std::string &in_relation = "==",
const std::string &in_val = "0x1")
const std::string &in_val = "")
: registerId(in_regId), hex_mask(in_hexMask), relation(in_relation),
hex_val(in_val) {}
hex_val(in_val.empty() ? in_hexMask : in_val) {}
int64_t registerId;
std::string hex_mask;
std::string relation;
......@@ -334,6 +334,11 @@ public:
++registerId;
return result;
}
void reset() {
memoryToRegister.clear();
registerId = 0;
}
};
class Instruction {
......
......@@ -302,7 +302,8 @@ TEST(AerAcceleratorTester, checkConditional) {
auto accelerator = xacc::getAccelerator("aer");
xacc::set_verbose(true);
auto xasmCompiler = xacc::getCompiler("xasm");
auto ir = xasmCompiler->compile(R"(__qpu__ void conditionalCirc(qbit q) {
{
auto ir = xasmCompiler->compile(R"(__qpu__ void conditionalCirc(qbit q) {
X(q[0]);
Measure(q[0]);
Measure(q[1]);
......@@ -320,15 +321,138 @@ TEST(AerAcceleratorTester, checkConditional) {
Measure(q[3]);
Measure(q[4]);
})",
accelerator);
accelerator);
auto program = ir->getComposite("conditionalCirc");
auto program = ir->getComposite("conditionalCirc");
auto buffer = xacc::qalloc(5);
accelerator->execute(buffer, program);
buffer->print();
// Expected: q0 = 1, q1 = 0, q2 = 1, q3 = 0, q4 = 1
EXPECT_EQ(buffer->computeMeasurementProbability("10101"), 1.0);
}
{
// Check Teleport
auto ir = xasmCompiler->compile(R"(__qpu__ void teleportOneState(qbit q) {
// State to be transported
X(q[0]);
// Bell channel setup
H(q[1]);
CX(q[1], q[2]);
// Alice Bell measurement
CX(q[0], q[1]);
H(q[0]);
Measure(q[0]);
Measure(q[1]);
// Correction
if (q[0])
{
Z(q[2]);
}
if (q[1])
{
X(q[2]);
}
// Measure teleported qubit
Measure(q[2]);
})",
accelerator);
auto program = ir->getComposite("teleportOneState");
auto buffer = xacc::qalloc(3);
accelerator->execute(buffer, program);
buffer->print();
for (const auto &bitStr : buffer->getMeasurements()) {
EXPECT_EQ(bitStr.length(), 3);
// q[2] (MSB) must be '1' (teleported)
EXPECT_EQ(bitStr[0], '1');
}
}
{
// Check Teleport
auto ir = xasmCompiler->compile(R"(__qpu__ void teleportZeroState(qbit q) {
// Bell channel setup
H(q[1]);
CX(q[1], q[2]);
// Alice Bell measurement
CX(q[0], q[1]);
H(q[0]);
Measure(q[0]);
Measure(q[1]);
// Correction
if (q[0])
{
Z(q[2]);
}
if (q[1])
{
X(q[2]);
}
// Measure teleported qubit
Measure(q[2]);
})",
accelerator);
auto program = ir->getComposite("teleportZeroState");
auto buffer = xacc::qalloc(3);
accelerator->execute(buffer, program);
buffer->print();
for (const auto &bitStr : buffer->getMeasurements()) {
EXPECT_EQ(bitStr.length(), 3);
// q[2] (MSB) must be '0' (teleported)
EXPECT_EQ(bitStr[0], '0');
}
}
{
// Check Teleport (50-50)
auto ir = xasmCompiler->compile(
R"(__qpu__ void teleportSuperpositionState(qbit q) {
// State to be transported
// superposition of 0 and 1
H(q[0]);
// Bell channel setup
H(q[1]);
CX(q[1], q[2]);
// Alice Bell measurement
CX(q[0], q[1]);
H(q[0]);
Measure(q[0]);
Measure(q[1]);
// Correction
if (q[0])
{
Z(q[2]);
}
if (q[1])
{
X(q[2]);
}
// Measure teleported qubit
Measure(q[2]);
})",
accelerator);
auto program = ir->getComposite("teleportSuperpositionState");
auto buffer = xacc::qalloc(3);
accelerator->execute(buffer, program);
buffer->print();
double zeroProb = 0.0;
double oneProb = 0.0;
for (const auto &bitStr : buffer->getMeasurements()) {
EXPECT_EQ(bitStr.length(), 3);
// q[2] (MSB) is the teleported qubit
if (bitStr[0] == '0') {
zeroProb += buffer->computeMeasurementProbability(bitStr);
} else {
oneProb += buffer->computeMeasurementProbability(bitStr);
}
}
EXPECT_NEAR(zeroProb, 0.5, 0.1);
EXPECT_NEAR(oneProb, 0.5, 0.1);
}
auto buffer = xacc::qalloc(5);
accelerator->execute(buffer, program);
buffer->print();
// Expected: q0 = 1, q1 = 0, q2 = 1, q3 = 0, q4 = 1
EXPECT_EQ(buffer->computeMeasurementProbability("10101"), 1.0);
xacc::set_verbose(false);
}
......
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