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

Ported InternalCompilerTester unit test



Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent a442c950
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -7,3 +7,12 @@ add_executable(QCORTester QCORTester.cpp)
add_test(NAME qcor_QCORTester COMMAND QCORTester)
target_include_directories(QCORTester PRIVATE ${XACC_ROOT}/include/gtest)
target_link_libraries(QCORTester ${XACC_TEST_LIBRARIES} qcor)


configure_file(InternalCompilerTester.in.cpp
               ${CMAKE_BINARY_DIR}/runtime/tests/InternalCompilerTester.cpp)
add_executable(InternalCompilerTester ${CMAKE_BINARY_DIR}/runtime/tests/InternalCompilerTester.cpp)
target_include_directories(InternalCompilerTester PRIVATE ${XACC_ROOT}/include/gtest ${CMAKE_SOURCE_DIR}/runtime/qrt/internal_compiler)
target_link_libraries(InternalCompilerTester PRIVATE xacc qrt ${XACC_TEST_LIBRARIES})
add_test(NAME QCORInternalCompilerTester COMMAND InternalCompilerTester)
target_compile_features(InternalCompilerTester PRIVATE cxx_std_17)
+125 −0
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2018 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
 *
 * Contributors:
 *   Alexander J. McCaskey - initial API and implementation
 *******************************************************************************/
#include <gtest/gtest.h>

#include "xacc.hpp"
#include "xacc_internal_compiler.hpp"
#include <qalloc>

using namespace xacc::internal_compiler;

TEST(InternalCompilerTester, checkMultipleBuffers) {

  auto q = qalloc(2);
  q.setName("q");

  auto r = qalloc(2);
  r.setName("r");

  auto circuit = compile("xasm", R"(__qpu__ void bell_x_2(qreg q, qreg r) {
  // First bell state on qbits 0,1
  H(q[0]);
  CX(q[0],q[1]);

  // Second bell state on qbis 2,3
  H(r[0]);
  CX(r[0],r[1]);

  // Measure them all
  Measure(q[0]);
  Measure(q[1]);

  Measure(r[0]);
  Measure(r[1]);
  // should see 0000, 0011, 1100, 1111
})");

  xacc::AcceleratorBuffer *bufs[2] = {q.results(), r.results()};

  execute(bufs, 2, circuit);
  auto counts = q.counts();
  for (const auto &kv : counts) {
    printf("%s: %i\n", kv.first.c_str(), kv.second);
  }
  counts = r.counts();
  for (const auto &kv : counts) {
    printf("%s: %i\n", kv.first.c_str(), kv.second);
  }
}

TEST(InternalCompilerTester, checkStaqAdd) {
  if (!xacc::hasCompiler("staq")) {
    return;
  }

  //   xacc::external::load_external_language_plugins();
  if (!xacc::hasAccelerator("aer")) {
    return;
  }
  setAccelerator("aer");

  auto a = qalloc(4);
  a.setName("a");
  a.store();

  auto b = qalloc(4);
  b.setName("b");
  b.store();

  auto c = qalloc(4);
  c.setName("c");
  c.store();

  auto src = R"(__qpu__ void add(qreg a, qreg b, qreg c) {
      oracle adder a0,a1,a2,a3,b0,b1,b2,b3,c0,c1,c2,c3 { "@CMAKE_SOURCE_DIR@/runtime/tests/adder_4.v" }
      creg result[4];
      // a = 3
      x a[0];
      x a[1];

      // b = 5
      x b[0];
      x b[2];

      adder a[0],a[1],a[2],a[3],b[0],b[1],b[2],b[3],c[0],c[1],c[2],c[3];

      // measure
      measure c -> result;
    })";
  auto circuit = compile("staq", src);

  //   optimize(circuit);

  xacc::AcceleratorBuffer *bufs[3] = {a.results(), b.results(), c.results()};

  //   std::vector<xacc::AcceleratorBuffer*> bufs{q.results(),r.results()};
  execute(bufs, 3, circuit);
  // Get the counts of the result buffer
  // should be 1000
  auto counts = c.counts();
  for (const auto &kv : counts) {
    printf("%s: %i\n", kv.first.c_str(), kv.second);
  }

  xacc::external::unload_external_language_plugins();
}

int main(int argc, char **argv) {
  compiler_InitializeXACC();
  //   xacc::set_verbose(true);

  ::testing::InitGoogleTest(&argc, argv);
  auto ret = RUN_ALL_TESTS();
  // xacc::Finalize();
  return ret;
}
+32 −0
Original line number Diff line number Diff line
module top (\a[0],\a[1],\a[2],\a[3],\b[0],\b[1],\b[2],\b[3],\c[0],\c[1],\c[2],\c[3]);
  input \a[0],\a[1],\a[2],\a[3],\b[0],\b[1],\b[2],\b[3];
  output \c[0],\c[1],\c[2],\c[3];
  wire n386,n387,n388,n389,n390,n391,n392,n393,n394,n395,n396,n397,n398,n399,n400,n401,n402,n403,n404,n405,n406,n407,n408,n409,n410 ;
  assign n386 = \a[0]  & ~\b[0] ;
  assign n387 = ~\a[0]  & \b[0] ;
  assign \c[0]  = n386 | n387;
  assign n389 = \a[0]  & \b[0] ;
  assign n390 = ~\a[1]  & ~\b[1] ;
  assign n391 = \a[1]  & \b[1] ;
  assign n392 = ~n390 & ~n391;
  assign n393 = n389 & ~n392;
  assign n394 = ~n389 & n392;
  assign \c[1]  = n393 | n394;
  assign n396 = n389 & ~n390;
  assign n397 = ~n391 & ~n396;
  assign n398 = ~\a[2]  & ~\b[2] ;
  assign n399 = \a[2]  & \b[2] ;
  assign n400 = ~n398 & ~n399;
  assign n401 = n397 & ~n400;
  assign n402 = ~n397 & n400;
  assign \c[2]  = ~n401 & ~n402;
  assign n404 = ~n397 & ~n398;
  assign n405 = ~n399 & ~n404;
  assign n406 = ~\a[3]  & ~\b[3] ;
  assign n407 = \a[3]  & \b[3] ;
  assign n408 = ~n406 & ~n407;
  assign n409 = n405 & ~n408;
  assign n410 = ~n405 & n408;
  assign \c[3]  = ~n409 & ~n410;
endmodule