QuilToXACCListener.cpp 2.7 KB
Newer Older
1
2
3
4
5
6
7
8
/*******************************************************************************
 * Copyright (c) 2019 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
 * distribution. The Eclipse Public License is available at
 * http://www.eclipse.org/legal/epl-v10.html and the Eclipse Distribution
 *License is available at https://eclipse.org/org/documents/edl-v10.php
Mccaskey, Alex's avatar
Mccaskey, Alex committed
9
10
 *
 * Contributors:
11
12
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
Mccaskey, Alex's avatar
Mccaskey, Alex committed
13
14
15
#include "exprtk.hpp"
#include "QuilToXACCListener.hpp"
#include "QuilBaseListener.h"
16
#include "xacc.hpp"
Mccaskey, Alex's avatar
Mccaskey, Alex committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "IRProvider.hpp"
#include "IR.hpp"
#include "xacc_service.hpp"

using namespace quil;

namespace xacc {
namespace quantum {

QuilToXACCListener::QuilToXACCListener() {
  gateRegistry = xacc::getService<IRProvider>("quantum");
}

void QuilToXACCListener::enterXacckernel(
    quil::QuilParser::XacckernelContext *ctx) {
  std::vector<std::string> params;
  for (int i = 0; i < ctx->typedparam().size(); i++) {
    params.push_back(
        ctx->typedparam(i)->IDENTIFIER()->getText());
  }
  function =
      gateRegistry->createComposite(ctx->kernelname->getText(), params);
}

41
42
43
44
45
46
47
void QuilToXACCListener::exitKernelcall(
    quil::QuilParser::KernelcallContext *ctx) {
  std::string gateName = ctx->kernelname->getText();
  auto f = xacc::getCompiled(gateName);
  function->addInstruction(f);
}

Mccaskey, Alex's avatar
Mccaskey, Alex committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
void QuilToXACCListener::exitGate(quil::QuilParser::GateContext *ctx) {
  std::string gateName = ctx->name()->getText();
  if (gateName == "RX")
    gateName = "Rx";
  if (gateName == "RY")
    gateName = "Ry";
  if (gateName == "RZ")
    gateName = "Rz";
  if (gateName == "CX")
    gateName = "CNOT";

  std::vector<std::size_t> qubits;
  for (int i = 0; i < ctx->qubit().size(); i++) {
    qubits.push_back(std::stoi(ctx->qubit(i)->getText()));
  }

  std::vector<InstructionParameter> params;
 for (int i = 0; i < ctx->param().size(); i++) {
    auto paramStr = ctx->param(i)->getText();
    params.emplace_back(paramStr);
  }

  std::shared_ptr<xacc::Instruction> instruction =
      gateRegistry->createInstruction(gateName, qubits, params);

  function->addInstruction(instruction);
}

void QuilToXACCListener::exitMeasure(quil::QuilParser::MeasureContext *ctx) {
  std::vector<std::size_t> qubits;
  qubits.push_back(std::stoi(ctx->qubit()->getText()));
  std::shared_ptr<xacc::Instruction> instruction =
      gateRegistry->createInstruction("Measure", qubits);
  function->addInstruction(instruction);
}
} // namespace quantum
} // namespace xacc