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

Fixed a nasty bug with adjoint



CPhase gate was checked using a wrong name....

Detect by phase adder circuit with compute action.

Signed-off-by: default avatarThien Nguyen <nguyentm@ornl.gov>
parent b8d77825
Loading
Loading
Loading
Loading
+44 −2
Original line number Diff line number Diff line
#pragma once
#include "qcor.hpp"
#include <qcor_qft>

namespace qcor {
namespace internal {
// Generates a list of angles to perform addition by a in the Fourier space.
void genAngles(std::vector<double> &io_angles, int a, int nbQubits) {
  // Makes sure the vector appropriately sized
  io_angles.resize(nbQubits);
  std::fill(io_angles.begin(), io_angles.end(), 0.0);

  for (int i = 0; i < nbQubits; ++i) {
    int bitIdx = nbQubits - i - 1;
    auto &angle = io_angles[bitIdx];
    for (int j = i; j < nbQubits; ++j) {
      // Check bit
      int bitShift = nbQubits - j - 1;
      if (((1 << bitShift) & a) != 0) {
        angle += std::pow(2.0, -(j - i));
      }
    }
    angle *= M_PI;
  }
}
} // namespace internal
} // namespace qcor

// Majority gate
__qpu__ void majority(qubit a, qubit b, qubit c) {
@@ -41,3 +65,21 @@ __qpu__ void integer_init(qreg a, int val) {
    }
  }
}

// Add an integer to the phase (Fourier basis)
__qpu__ void phase_add_integer(qreg q, int a) {
  std::vector<double> angles;
  qcor::internal::genAngles(angles, a, q.size());
  for (int i = 0; i < q.size(); ++i) {
    U1(q[i], angles[i]);
  }
}

// Add an integer to the a qubit register:
// |a> --> |a + b>
__qpu__ void add_integer(qreg q, int a) {
  // Bring it to Fourier basis
  // (IQFT automatically)
  compute { qft_opt_swap(q, 0); }
  action { phase_add_integer(q, a); }
}
 No newline at end of file
+1 −1
Original line number Diff line number Diff line
@@ -715,7 +715,7 @@ void apply_adjoint(std::shared_ptr<CompositeInstruction> parent_kernel,
    auto inst = tempKernel->getInstruction(i);
    // Parametric gates:
    if (inst->name() == "Rx" || inst->name() == "Ry" || inst->name() == "Rz" ||
        inst->name() == "CPHASE" || inst->name() == "U1" ||
        inst->name() == "CPhase" || inst->name() == "U1" ||
        inst->name() == "CRZ") {
      inst->setParameter(0, -inst->getParameter(0).template as<double>());
    }