QuilVisitorTester.cpp 2.74 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
16
#include <memory>
#include <gtest/gtest.h>
#include "QuilVisitor.hpp"
#include "InstructionIterator.hpp"
17
#include "xacc.hpp"
18
#include "Circuit.hpp"
Mccaskey, Alex's avatar
Mccaskey, Alex committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
85
86

using namespace xacc;
using namespace xacc::quantum;

TEST(QuilVisitorTester, checkIRToQuil) {

  auto f = std::make_shared<Circuit>("foo");

  auto x = std::make_shared<X>(0);
  auto h = std::make_shared<Hadamard>(1);
  auto cn1 = std::make_shared<CNOT>(1, 2);
  auto cn2 = std::make_shared<CNOT>(0, 1);
  auto h2 = std::make_shared<Hadamard>(0);
  auto m0 = std::make_shared<Measure>(0, 0);
  auto m1 = std::make_shared<Measure>(1, 1);

//   auto cond1 = std::make_shared<ConditionalFunction>(0);
//   auto z = std::make_shared<Z>(2);
//   cond1->addInstruction(z);
//   auto cond2 = std::make_shared<ConditionalFunction>(1);
//   auto x2 = std::make_shared<X>(2);
//   cond2->addInstruction(x2);

  f->addInstruction(x);
  f->addInstruction(h);
  f->addInstruction(cn1);
  f->addInstruction(cn2);
  f->addInstruction(h2);
  f->addInstruction(m0);
  f->addInstruction(m1);
//   f->addInstruction(cond1);
//   f->addInstruction(cond2);

  // Create the Instruction Visitor that is going
  // to map our IR to Quil.
  auto visitor = std::make_shared<QuilVisitor>();

  // Our QIR is really a tree structure
  // so create a pre-order tree traversal
  // InstructionIterator to walk it
  InstructionIterator it(f);
  while (it.hasNext()) {
    // Get the next node in the tree
    auto nextInst = it.next();
    if (nextInst->isEnabled())
      nextInst->accept(visitor);
  }

  std::cout << visitor->getQuilString() << "\n";

//   std::string expectedQuil = "X 0\n"
//                              "H 1\n"
//                              "CNOT 1 2\n"
//                              "CNOT 0 1\n"
//                              "H 0\n"
//                              "MEASURE 0 [0]\n"
//                              "MEASURE 1 [1]\n;

//   EXPECT_TRUE(expectedQuil == visitor->getQuilString());
}

int main(int argc, char **argv) {
  xacc::Initialize();
  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  xacc::Finalize();
  return ret;
}