Commit 38192020 authored by Nguyen, Thien Minh's avatar Nguyen, Thien Minh
Browse files

Added fSim and iSwap to Qpp Accelerator


Signed-off-by: Nguyen, Thien Minh's avatarThien Nguyen <nguyentm@ornl.gov>
parent 6ac3bd82
......@@ -14,6 +14,32 @@
#include "QppVisitor.hpp"
#include "xacc.hpp"
namespace {
// Add gate matrix for iSwap and fSim gates
qpp::cmat iSwapGateMat()
{
qpp::cmat gateMat(4, 4);
gateMat << 1.0, 0.0, 0.0, 0.0,
0.0, 0.0, std::complex<double>(0, 1.0), 0.0,
0.0, std::complex<double>(0, 1.0), 0.0, 0.0,
0.0, 0.0, 0.0, 1.0;
return gateMat;
}
qpp::cmat fSimGateMat(double in_theta, double in_phi)
{
qpp::cmat gateMat(4, 4);
gateMat <<
1.0, 0.0, 0.0 , 0.0 ,
0.0, std::cos(in_theta), std::complex<double>(0, -std::sin(in_theta)), 0.0,
0.0, std::complex<double>(0, -std::sin(in_theta)), std::cos(in_theta), 0.0,
0.0, 0.0, 0.0, std::exp(std::complex<double>(0, -in_phi));
return gateMat;
}
}
namespace xacc {
namespace quantum {
void QppVisitor::initialize(std::shared_ptr<AcceleratorBuffer> buffer, bool shotsMode)
......@@ -203,6 +229,24 @@ namespace quantum {
m_stateVec = qpp::apply(m_stateVec, uMat, { qubitIdx });
}
void QppVisitor::visit(iSwap& in_iSwapGate)
{
const auto qIdx1 = xaccIdxToQppIdx(in_iSwapGate.bits()[0]);
const auto qIdx2 = xaccIdxToQppIdx(in_iSwapGate.bits()[1]);
m_stateVec = qpp::apply(m_stateVec, iSwapGateMat(), { qIdx1, qIdx2 });
}
void QppVisitor::visit(fSim& in_fsimGate)
{
const auto qIdx1 = xaccIdxToQppIdx(in_fsimGate.bits()[0]);
const auto qIdx2 = xaccIdxToQppIdx(in_fsimGate.bits()[1]);
const auto theta = InstructionParameterToDouble(in_fsimGate.getParameter(0));
const auto phi = InstructionParameterToDouble(in_fsimGate.getParameter(1));
m_stateVec = qpp::apply(m_stateVec, fSimGateMat(theta, phi), { qIdx1, qIdx2 });
}
void QppVisitor::visit(Measure& measure)
{
if (xacc::verbose)
......
......@@ -50,7 +50,9 @@ public:
void visit(Identity& i) override;
void visit(U& u) override;
void visit(IfStmt& ifStmt) override;
void visit(iSwap& in_iSwapGate) override;
void visit(fSim& in_fsimGate) override;
virtual std::shared_ptr<QppVisitor> clone() { return std::make_shared<QppVisitor>(); }
private:
......
......@@ -397,6 +397,62 @@ TEST(QppAcceleratorTester, testConditional)
EXPECT_EQ(resultCount, nbTests);
}
TEST(QppAcceleratorTester, testISwap)
{
// Get reference to the Accelerator
xacc::set_verbose(false);
const int nbShots = 100;
auto accelerator = xacc::getAccelerator("qpp", { std::make_pair("shots", nbShots) });
auto xasmCompiler = xacc::getCompiler("xasm");
auto ir = xasmCompiler->compile(R"(__qpu__ void testISwap(qbit q) {
X(q[0]);
iSwap(q[0], q[3]);
Measure(q[0]);
Measure(q[1]);
Measure(q[2]);
Measure(q[3]);
Measure(q[4]);
})", accelerator);
auto program = ir->getComposite("testISwap");
// Allocate some qubits (5)
auto buffer = xacc::qalloc(5);
accelerator->execute(buffer, program);
// 10000 => i00010 after iswap
buffer->print();
EXPECT_EQ(buffer->getMeasurementCounts()["00010"], nbShots);
}
TEST(QppAcceleratorTester, testFsim)
{
// Get reference to the Accelerator
const int nbShots = 1000;
auto accelerator = xacc::getAccelerator("qpp", { std::make_pair("shots", nbShots) });
auto xasmCompiler = xacc::getCompiler("xasm");
auto ir = xasmCompiler->compile(R"(__qpu__ void testFsim(qbit q, double x, double y) {
X(q[0]);
fSim(q[0], q[2], x, y);
Measure(q[0]);
Measure(q[2]);
})", accelerator);
auto program = ir->getComposites()[0];
const auto angles = xacc::linspace(-xacc::constants::pi, xacc::constants::pi, 10);
for (const auto& a : angles)
{
auto buffer = xacc::qalloc(3);
auto evaled = program->operator()({ a, 0.0 });
accelerator->execute(buffer, evaled);
const auto expectedProb = std::sin(a) * std::sin(a);
std::cout << "Angle = " << a << "\n";
buffer->print();
// fSim mixes 01 and 10 states w.r.t. the theta angle.
EXPECT_NEAR(buffer->computeMeasurementProbability("01"), expectedProb, 0.1);
EXPECT_NEAR(buffer->computeMeasurementProbability("10"), 1.0 - expectedProb, 0.1);
}
}
int main(int argc, char **argv) {
xacc::Initialize();
......
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