QNLP  v1.0
QNLP::SimulatorGeneral< DerivedType > Class Template Reference

CRTP defined class for simulator implementations. More...

#include <Simulator.hpp>

Inheritance diagram for QNLP::SimulatorGeneral< DerivedType >:
Inheritance graph
Collaboration diagram for QNLP::SimulatorGeneral< DerivedType >:
Collaboration graph

Public Member Functions

virtual ~SimulatorGeneral ()
 Destroy the Simulator General object. More...
 
void applyGateX (std::size_t qubit_idx)
 Apply the Pauli X gate to the given qubit. More...
 
void applyGateY (std::size_t qubit_idx)
 Apply the Pauli Y gate to the given qubit. More...
 
void applyGateZ (std::size_t qubit_idx)
 Apply the Pauli Z gate to the given qubit. More...
 
void applyGateI (std::size_t qubit_idx)
 Apply the Identity gate to the given qubit. More...
 
void applyGateH (std::size_t qubit_idx)
 Apply the Hadamard gate to the given qubit. More...
 
void applyGateSqrtX (std::size_t qubit_idx)
 Apply the Sqrt{Pauli X} gate to the given qubit. More...
 
void applyGateRotX (std::size_t qubit_idx, double angle_rad)
 Apply the given Rotation about X-axis to the given qubit. More...
 
void applyGateRotY (std::size_t qubit_idx, double angle_rad)
 Apply the given Rotation about Y-axis to the given qubit. More...
 
void applyGateRotZ (std::size_t qubit_idx, double angle_rad)
 Apply the given Rotation about Z-axis to the given qubit. More...
 
template<class Mat2x2Type >
void applyGateU (const Mat2x2Type &U, std::size_t qubit_idx)
 Apply arbitrary user-defined unitary gate to qubit at qubit_idx. More...
 
decltype(auto) getGateX ()
 Get the Pauli-X gate; returns templated type GateType. More...
 
decltype(auto) getGateY ()
 Get the Pauli-Y gate; must be overloaded with appropriate return type. More...
 
decltype(auto) getGateZ ()
 Get the Pauli-Z gate; must be overloaded with appropriate return type. More...
 
decltype(auto) getGateI ()
 Get the Identity; must be overloaded with appropriate return type. More...
 
decltype(auto) getGateH ()
 Get the Hadamard gate; must be overloaded with appropriate return type. More...
 
template<class Mat2x2Type >
void applyGateCU (const Mat2x2Type &U, const std::size_t control, const std::size_t target, std::string label="CU")
 Apply the given controlled unitary gate on target qubit. More...
 
void applyGateCX (const std::size_t control, const std::size_t target)
 Apply Controlled Pauli-X (CNOT) on target qubit. More...
 
void applyGateCY (const std::size_t control, const std::size_t target)
 Apply Controlled Pauli-Y on target qubit. More...
 
void applyGateCZ (const std::size_t control, const std::size_t target)
 Apply Controlled Pauli-Z on target qubit. More...
 
void applyGateCH (const std::size_t control, const std::size_t target)
 Apply Controlled Hadamard on target qubit. More...
 
void applyGateSwap (std::size_t qubit_idx0, std::size_t qubit_idx1)
 Swap the qubits at the given indices. More...
 
void applyGateSqrtSwap (std::size_t qubit_idx0, std::size_t qubit_idx1)
 Performs Sqrt SWAP gate between two given qubits (half way SWAP) More...
 
void applyGatePhaseShift (double angle, std::size_t qubit_idx)
 Apply phase shift to given Qubit; [[1 0] [0 exp(i*angle)]]. More...
 
void applyGateCPhaseShift (double angle, std::size_t control, std::size_t target)
 Perform controlled phase shift gate. More...
 
void applyGateCCX (std::size_t ctrl_qubit0, std::size_t ctrl_qubit1, std::size_t target_qubit)
 Controlled controlled NOT (CCNOT, CCX) gate. More...
 
void applyGateCSwap (std::size_t ctrl_qubit, std::size_t qubit_swap0, std::size_t qubit_swap1)
 Controlled SWAP gate. More...
 
void applyGateCRotX (std::size_t ctrl_qubit, std::size_t qubit_idx, double angle_rad)
 Apply the given Controlled Rotation about X-axis to the given qubit. More...
 
void applyGateCRotY (std::size_t ctrl_qubit, std::size_t qubit_idx, double angle_rad)
 Apply the given Controlled Rotation about Y-axis to the given qubit. More...
 
void applyGateCRotZ (std::size_t ctrl_qubit, std::size_t qubit_idx, double angle_rad)
 Apply the given Controlled Rotation about Z-axis to the given qubit. More...
 
decltype(auto) getQubitRegister ()
 Get the underlying qubit register object. More...
 
std::size_t getNumQubits ()
 Get the number of Qubits. More...
 
void applyQFT (std::size_t minIdx, std::size_t maxIdx)
 Apply the forward Quantum Fourier transform (QFT) to the given register index range. More...
 
void applyIQFT (std::size_t minIdx, std::size_t maxIdx)
 Apply the inverse Quantum Fourier transform (IQFT) to the given register index range. More...
 
void sumReg (std::size_t r0_minIdx, std::size_t r0_maxIdx, std::size_t r1_minIdx, std::size_t r1_maxIdx)
 Applies |r1>|r2> -> |r1>|r1+r2> More...
 
void subReg (std::size_t r0_minIdx, std::size_t r0_maxIdx, std::size_t r1_minIdx, std::size_t r1_maxIdx)
 Applies |r1>|r2> -> |r1>|r1-r2> More...
 
template<class Mat2x2Type >
void applyGateNCU (const Mat2x2Type &U, const std::vector< std::size_t > &ctrlIndices, std::size_t target, std::string label)
 Apply n-control unitary gate to the given qubit target. More...
 
template<class Mat2x2Type >
void applyGateNCU (const Mat2x2Type &U, const std::vector< std::size_t > &ctrlIndices, const std::vector< std::size_t > &auxIndices, std::size_t target, std::string label)
 Apply n-control sigma_x gate to the given qubit target, using auxiliary qubits for 5CX optimisation. More...
 
template<class Mat2x2Type >
void applyOracleU (std::size_t bit_pattern, const std::vector< std::size_t > &ctrlIndices, std::size_t target, const Mat2x2Type &U, std::string gateLabel)
 Apply oracle to match given binary index with non adjacent controls. More...
 
template<class Mat2x2Type >
void applyOracleU (std::size_t bit_pattern, const std::vector< std::size_t > &ctrlIndices, const std::vector< std::size_t > &auxIndices, std::size_t target, const Mat2x2Type &U, std::string gateLabel)
 Apply oracle to match given binary index with non adjacent controls. More...
 
void applyOraclePhase (std::size_t bit_pattern, const std::vector< std::size_t > &ctrlIndices, std::size_t target)
 Apply oracle to match given binary index with linearly adjacent controls. More...
 
void applyDiffusion (const std::vector< std::size_t > &ctrlIndices, std::size_t target)
 Apply diffusion operator on marked state. More...
 
void encodeToRegister (std::size_t target_pattern, const std::vector< std::size_t > target_register, std::size_t len_bin_pattern)
 Encodes a defined binary pattern into a defined target register (initially in state |00...0>) of all quantum states in the superposition. More...
 
void encodeBinToSuperpos_unique (const std::vector< std::size_t > &reg_memory, const std::vector< std::size_t > &reg_auxiliary, const std::vector< std::size_t > &bin_patterns, const std::size_t len_bin_pattern)
 Encode inputted binary strings to the memory register specified as a superposition of states. Note that this implementation does not allow for multiple instances of the same input pattern but allows for 0 to be encoded. More...
 
void applyHammingDistanceRotY (std::size_t test_pattern, const std::vector< std::size_t > reg_mem, const std::vector< std::size_t > reg_auxiliary, std::size_t len_bin_pattern)
 Computes the relative Hamming distance between the test pattern and the pattern stored in each state of the superposition, storing the result in the amplitude of the corresponding state. More...
 
void applyHammingDistanceOverwrite (std::size_t test_pattern, const std::vector< std::size_t > reg_mem, const std::vector< std::size_t > reg_auxiliary, std::size_t len_bin_pattern)
 Computes the relative Hamming distance between the test pattern and the pattern stored in each state of the superposition, overwriting the aux register pattern with the resulting bit differences. More...
 
bool applyMeasurement (std::size_t target, bool normalize=true)
 Apply measurement to a target qubit, randomly collapsing the qubit proportional to the amplitude and returns the collapsed value. More...
 
std::size_t applyMeasurementToRegister (std::vector< std::size_t > target_qubits, bool normalize=true)
 Apply measurement to a set of target qubits, randomly collapsing the qubits proportional to the amplitude and returns the bit string of the qubits in the order they are represented in the vector of indexes, in the form of an unsigned integer. More...
 
void groupQubits (const std::vector< std::size_t > reg_auxiliary, bool lsb=true)
 Group all set qubits to MSB in register (ie |010100> -> |000011>) More...
 
void collapseToBasisZ (std::size_t target, bool collapseValue)
 Apply measurement to a target qubit with respect to the Z-basis, collapsing to a specified value (0 or 1). Amplitudes are r-normalized afterwards. More...
 
void initRegister ()
 (Re)Initialise the underlying register of the encapsulated simulator to well-defined state (|0....0>) More...
 
void initCaches ()
 Initialise caches used in NCU operation. More...
 
template<class Mat2x2Type >
void addUToCache (std::string gateLabel, const Mat2x2Type &U)
 Adds a matrix to the cache, assigning it to a defined label. This is used in the caching for the NCU operation. More...
 
void PrintStates (std::string x, std::vector< std::size_t > qubits={})
 Prints the string x and then for each state of the specified qubits in the superposition, prints each its amplitude, followed by state and then by the probability of that state. Note that this state observation method is not a permitted quantum operation, however it is provided for convenience and debugging/testing. More...
 
template<class Mat2x2Type >
Mat2x2Type matrixSqrt (const Mat2x2Type &U)
 Calculates the unitary matrix square root (U == VV, where V is returned) More...
 
void InvertRegister (const unsigned int minIdx, const unsigned int maxIdx)
 Invert the register about the given indides: 0,1,2...n-1,n -> n,n-1,...,1,0. More...
 

Static Public Member Functions

template<class Mat2x2Type >
static Mat2x2Type adjointMatrix (const Mat2x2Type &U)
 Function to calculate the adjoint of an input matrix. More...
 

Protected Attributes

std::any sim_ncu
 

Private Member Functions

 SimulatorGeneral ()
 Construct a new Simulator General object. More...
 

Private Attributes

friend DerivedType
 

Detailed Description

template<class DerivedType>
class QNLP::SimulatorGeneral< DerivedType >

CRTP defined class for simulator implementations.

Template Parameters
DerivedTypeCRTP derived class simulator type

Definition at line 69 of file Simulator.hpp.

Constructor & Destructor Documentation

◆ SimulatorGeneral()

template<class DerivedType>
QNLP::SimulatorGeneral< DerivedType >::SimulatorGeneral ( )
inlineprivate

Construct a new Simulator General object.

Definition at line 77 of file Simulator.hpp.

77  {
78  //If we are building MPI support, ensure that it is init'd here before subclasses.
79  #ifdef ENABLE_MPI
80  int mpi_is_init;
81  MPI_Initialized(&mpi_is_init);
82  if (! mpi_is_init){
83  int argc_tmp = 0;
84  char** argv_tmp = new char*[argc_tmp];
85  MPI_Init(&argc_tmp, &argv_tmp);
86  delete argv_tmp;
87  }
88  #endif
89 
90  sim_ncu = NCU<DerivedType>(static_cast<DerivedType&>(*this));
91  };

◆ ~SimulatorGeneral()

template<class DerivedType>
virtual QNLP::SimulatorGeneral< DerivedType >::~SimulatorGeneral ( )
inlinevirtual

Destroy the Simulator General object.

Definition at line 113 of file Simulator.hpp.

113 { }

Member Function Documentation

◆ addUToCache()

template<class DerivedType>
template<class Mat2x2Type >
void QNLP::SimulatorGeneral< DerivedType >::addUToCache ( std::string  gateLabel,
const Mat2x2Type &  U 
)
inline

Adds a matrix to the cache, assigning it to a defined label. This is used in the caching for the NCU operation.

Template Parameters
Mat2x2TypeMatrix type to be cached
Parameters
gateLabelLabel assigned to the matrix being cached
UMatrix to be cached

Definition at line 703 of file Simulator.hpp.

703  {
704  #if defined(__INTEL_COMPILER) || defined(__INTEL_LLVM_COMPILER)
705  std::experimental::any_cast<NCU<DerivedType>&>(sim_ncu).getGateCache().addToCache(static_cast<DerivedType&>(*this), gateLabel, U, 16);
706  #else
707  std::any_cast<NCU<DerivedType>&>(sim_ncu).getGateCache().addToCache(static_cast<DerivedType&>(*this), gateLabel, U, 16);
708  #endif
709  }

◆ adjointMatrix()

template<class DerivedType>
template<class Mat2x2Type >
static Mat2x2Type QNLP::SimulatorGeneral< DerivedType >::adjointMatrix ( const Mat2x2Type &  U)
inlinestatic

Function to calculate the adjoint of an input matrix.

Template Parameters
Mat2x2TypeMatrix type
Parameters
UUnitary matrix to be adjointed
Returns
openqu::TinyMatrix<Type, 2, 2, 32> U^{\dagger}

Definition at line 757 of file Simulator.hpp.

757  {
758  Mat2x2Type Uadjoint(U);
759  std::complex<double> tmp;
760  tmp = Uadjoint(0,1);
761  Uadjoint(0,1) = Uadjoint(1,0);
762  Uadjoint(1,0) = tmp;
763  Uadjoint(0,0) = std::conj(Uadjoint(0,0));
764  Uadjoint(0,1) = std::conj(Uadjoint(0,1));
765  Uadjoint(1,0) = std::conj(Uadjoint(1,0));
766  Uadjoint(1,1) = std::conj(Uadjoint(1,1));
767  return Uadjoint;
768  }

◆ applyDiffusion()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyDiffusion ( const std::vector< std::size_t > &  ctrlIndices,
std::size_t  target 
)
inline

Apply diffusion operator on marked state.

Parameters
ctrlIndicesVector of control line indices
targetTarget qubit index to apply Ctrl-Z upon.

Definition at line 536 of file Simulator.hpp.

536  {
537  Diffusion<DerivedType> diffusion;
538  diffusion.applyOpDiffusion(static_cast<DerivedType&>(*this), ctrlIndices, target);
539  }

◆ applyGateCCX()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateCCX ( std::size_t  ctrl_qubit0,
std::size_t  ctrl_qubit1,
std::size_t  target_qubit 
)
inline

Controlled controlled NOT (CCNOT, CCX) gate.

Parameters
ctrl_qubit0Control qubit 0
ctrl_qubit1Control qubit 1
target_qubitTarget qubit

Definition at line 346 of file Simulator.hpp.

346  {
347  static_cast<DerivedType*>(this)->applyGateCCX(ctrl_qubit0, ctrl_qubit1, target_qubit);
348  }

◆ applyGateCH()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateCH ( const std::size_t  control,
const std::size_t  target 
)
inline

Apply Controlled Hadamard on target qubit.

Parameters
controlQubit index acting as control
targetQubit index acting as target

Definition at line 294 of file Simulator.hpp.

294  {
295  static_cast<DerivedType*>(this)->applyGateCH(control, target);
296  }

◆ applyGateCPhaseShift()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateCPhaseShift ( double  angle,
std::size_t  control,
std::size_t  target 
)
inline

Perform controlled phase shift gate.

Parameters
angleAngle of phase shift in rads
controlIndex of control qubit
targetIndex of target qubit

Definition at line 335 of file Simulator.hpp.

335  {
336  static_cast<DerivedType*>(this)->applyGateCPhaseShift(angle, control, target);
337  }

◆ applyGateCRotX()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateCRotX ( std::size_t  ctrl_qubit,
std::size_t  qubit_idx,
double  angle_rad 
)
inline

Apply the given Controlled Rotation about X-axis to the given qubit.

Parameters
ctrl_qubitControl qubit
qubit_idxIndex of qubit to rotate about X-axis
angle_radRotation angle

Definition at line 373 of file Simulator.hpp.

373  {
374  static_cast<DerivedType&>(*this).applyGateCRotX(ctrl_qubit, qubit_idx, angle_rad);
375  }

◆ applyGateCRotY()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateCRotY ( std::size_t  ctrl_qubit,
std::size_t  qubit_idx,
double  angle_rad 
)
inline

Apply the given Controlled Rotation about Y-axis to the given qubit.

Parameters
ctrl_qubitControl qubit
qubit_idxIndex of qubit to rotate about Y-axis
angle_radRotation angle

Definition at line 384 of file Simulator.hpp.

384  {
385  static_cast<DerivedType&>(*this).applyGateCRotY(ctrl_qubit, qubit_idx, angle_rad);
386  }

◆ applyGateCRotZ()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateCRotZ ( std::size_t  ctrl_qubit,
std::size_t  qubit_idx,
double  angle_rad 
)
inline

Apply the given Controlled Rotation about Z-axis to the given qubit.

Parameters
ctrl_qubitControl qubit
qubit_idxIndex of qubit to rotate about Z-axis
angle_radRotation angle

Definition at line 395 of file Simulator.hpp.

395  {
396  static_cast<DerivedType&>(*this).applyGateCRotZ(ctrl_qubit, qubit_idx, angle_rad);
397  }

◆ applyGateCSwap()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateCSwap ( std::size_t  ctrl_qubit,
std::size_t  qubit_swap0,
std::size_t  qubit_swap1 
)
inline

Controlled SWAP gate.

Parameters
ctrl_qubitControl qubit
qubit_swap0Swap qubit 0
qubit_swap1Swap qubit 1

Definition at line 357 of file Simulator.hpp.

357  {
358  assert( static_cast<DerivedType*>(this)->getNumQubits() > 2 );
359  //The requested qubit indices must be available to use within the register range
360  assert( (ctrl_qubit < getNumQubits() ) && (qubit_swap0 < getNumQubits() ) && (qubit_swap1 < getNumQubits()) );
361  //The qubits must be different from one another
362  assert( ctrl_qubit != qubit_swap0 && ctrl_qubit != qubit_swap1 && qubit_swap0 != qubit_swap1 );
363  static_cast<DerivedType*>(this)->applyGateCSwap(ctrl_qubit, qubit_swap0, qubit_swap1);
364  }
std::size_t getNumQubits()
Get the number of Qubits.
Definition: Simulator.hpp:413

◆ applyGateCU()

template<class DerivedType>
template<class Mat2x2Type >
void QNLP::SimulatorGeneral< DerivedType >::applyGateCU ( const Mat2x2Type &  U,
const std::size_t  control,
const std::size_t  target,
std::string  label = "CU" 
)
inline

Apply the given controlled unitary gate on target qubit.

Parameters
UUser-defined arbitrary 2x2 unitary gate (matrix)
controlQubit index acting as control
targetQubit index acting as target

Definition at line 254 of file Simulator.hpp.

254  {
255  static_cast<DerivedType*>(this)->applyGateCU(U, control, target, label);
256  }

◆ applyGateCX()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateCX ( const std::size_t  control,
const std::size_t  target 
)
inline

Apply Controlled Pauli-X (CNOT) on target qubit.

Parameters
controlQubit index acting as control
targetQubit index acting as target

Definition at line 264 of file Simulator.hpp.

264  {
265  static_cast<DerivedType*>(this)->applyGateCX(control, target);
266  }

◆ applyGateCY()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateCY ( const std::size_t  control,
const std::size_t  target 
)
inline

Apply Controlled Pauli-Y on target qubit.

Parameters
controlQubit index acting as control
targetQubit index acting as target

Definition at line 274 of file Simulator.hpp.

274  {
275  static_cast<DerivedType*>(this)->applyGateCY(control, target);
276  }

◆ applyGateCZ()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateCZ ( const std::size_t  control,
const std::size_t  target 
)
inline

Apply Controlled Pauli-Z on target qubit.

Parameters
controlQubit index acting as control
targetQubit index acting as target

Definition at line 284 of file Simulator.hpp.

284  {
285  static_cast<DerivedType*>(this)->applyGateCZ(control, target);
286  }

◆ applyGateH()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateH ( std::size_t  qubit_idx)
inline

Apply the Hadamard gate to the given qubit.

Parameters
qubit_idx

Definition at line 155 of file Simulator.hpp.

155  {
156  static_cast<DerivedType&>(*this).applyGateH(qubit_idx);
157  }

◆ applyGateI()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateI ( std::size_t  qubit_idx)
inline

Apply the Identity gate to the given qubit.

Parameters
qubit_idx

Definition at line 147 of file Simulator.hpp.

147  {
148  static_cast<DerivedType&>(*this).applyGateI(qubit_idx);
149  }

◆ applyGateNCU() [1/2]

template<class DerivedType>
template<class Mat2x2Type >
void QNLP::SimulatorGeneral< DerivedType >::applyGateNCU ( const Mat2x2Type &  U,
const std::vector< std::size_t > &  ctrlIndices,
std::size_t  target,
std::string  label 
)
inline

Apply n-control unitary gate to the given qubit target.

Template Parameters
Mat2x2Type2x2 Matrix type of unitary gate in the format expected by the derived simulator object; decltype(simulator.getGateX()) can be used in template
Parameters
U2x2 unitary matrix
ctrlIndicesVector of the control lines for NCU operation
targetTarget qubit index to apply nCU
labelGate label string (U, X, Y, etc.)

Definition at line 462 of file Simulator.hpp.

462  {
463  #if defined(__INTEL_COMPILER) || defined(__INTEL_LLVM_COMPILER)
464  std::experimental::any_cast<NCU<DerivedType>&>(sim_ncu).applyNQubitControl(static_cast<DerivedType&>(*this), ctrlIndices, {}, target, label, U, 0);
465  #else
466  std::any_cast<NCU<DerivedType>&>(sim_ncu).applyNQubitControl(static_cast<DerivedType&>(*this), ctrlIndices, {}, target, label, U, 0);
467  #endif
468  }

◆ applyGateNCU() [2/2]

template<class DerivedType>
template<class Mat2x2Type >
void QNLP::SimulatorGeneral< DerivedType >::applyGateNCU ( const Mat2x2Type &  U,
const std::vector< std::size_t > &  ctrlIndices,
const std::vector< std::size_t > &  auxIndices,
std::size_t  target,
std::string  label 
)
inline

Apply n-control sigma_x gate to the given qubit target, using auxiliary qubits for 5CX optimisation.

Template Parameters
Mat2x2Type2x2 Matrix type of unitary gate in the format expected by the derived simulator object; decltype(simulator.getGateX()) can be used in template
Parameters
U2x2 unitary matrix
minIdxLowest index of the control lines expected for nCU
maxIdxHighest index of the control lines expected for the nCU
targetTarget qubit index to apply nCU

Definition at line 480 of file Simulator.hpp.

480  {
481  #if defined(__INTEL_COMPILER) || defined(__INTEL_LLVM_COMPILER)
482  std::experimental::any_cast<NCU<DerivedType>&>(sim_ncu).applyNQubitControl(static_cast<DerivedType&>(*this), ctrlIndices, auxIndices, target, label, U, 0);
483  #else
484  std::any_cast<NCU<DerivedType>&>(sim_ncu).applyNQubitControl(static_cast<DerivedType&>(*this), ctrlIndices, auxIndices, target, label, U, 0);
485  #endif
486  }

◆ applyGatePhaseShift()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGatePhaseShift ( double  angle,
std::size_t  qubit_idx 
)
inline

Apply phase shift to given Qubit; [[1 0] [0 exp(i*angle)]].

Parameters
angleAngle of phase shift in rads
qubit_idxQubit index to perform phase shift upon

Definition at line 324 of file Simulator.hpp.

324  {
325  static_cast<DerivedType*>(this)->applyGatePhaseShift(angle, qubit_idx);
326  }

◆ applyGateRotX()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateRotX ( std::size_t  qubit_idx,
double  angle_rad 
)
inline

Apply the given Rotation about X-axis to the given qubit.

Parameters
qubit_idxIndex of qubit to rotate about X-axis
angle_radRotation angle

Definition at line 172 of file Simulator.hpp.

172  {
173  static_cast<DerivedType&>(*this).applyGateRotX(qubit_idx, angle_rad);
174  }

◆ applyGateRotY()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateRotY ( std::size_t  qubit_idx,
double  angle_rad 
)
inline

Apply the given Rotation about Y-axis to the given qubit.

Parameters
qubit_idxIndex of qubit to rotate about Y-axis
angle_radRotation angle

Definition at line 181 of file Simulator.hpp.

181  {
182  static_cast<DerivedType&>(*this).applyGateRotY(qubit_idx, angle_rad);
183  }

◆ applyGateRotZ()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateRotZ ( std::size_t  qubit_idx,
double  angle_rad 
)
inline

Apply the given Rotation about Z-axis to the given qubit.

Parameters
qubit_idxIndex of qubit to rotate about Z-axis
angle_radRotation angle

Definition at line 190 of file Simulator.hpp.

190  {
191  static_cast<DerivedType&>(*this).applyGateRotZ(qubit_idx, angle_rad);
192  }

◆ applyGateSqrtSwap()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateSqrtSwap ( std::size_t  qubit_idx0,
std::size_t  qubit_idx1 
)
inline

Performs Sqrt SWAP gate between two given qubits (half way SWAP)

Parameters
qubit_idx0Qubit index 0
qubit_idx1Qubit index 1

Definition at line 314 of file Simulator.hpp.

314  {
315  static_cast<DerivedType*>(this)->applyGateSqrtSwap(qubit_idx0,qubit_idx1);
316  }

◆ applyGateSqrtX()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateSqrtX ( std::size_t  qubit_idx)
inline

Apply the Sqrt{Pauli X} gate to the given qubit.

Parameters
qubit_idx

Definition at line 163 of file Simulator.hpp.

163  {
164  static_cast<DerivedType&>(*this).applyGateSqrtX(qubit_idx);
165  }

◆ applyGateSwap()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateSwap ( std::size_t  qubit_idx0,
std::size_t  qubit_idx1 
)
inline

Swap the qubits at the given indices.

Parameters
qubit_idx0Index of qubit 0 to swap &(0 -> 1)
qubit_idx1Index of qubit 1 to swap &(1 -> 0)

Definition at line 304 of file Simulator.hpp.

304  {
305  static_cast<DerivedType*>(this)->applyGateSwap(qubit_idx0,qubit_idx1);
306  }

Referenced by QNLP::SimulatorGeneral< IntelSimulator >::InvertRegister().

Here is the caller graph for this function:

◆ applyGateU()

template<class DerivedType>
template<class Mat2x2Type >
void QNLP::SimulatorGeneral< DerivedType >::applyGateU ( const Mat2x2Type &  U,
std::size_t  qubit_idx 
)
inline

Apply arbitrary user-defined unitary gate to qubit at qubit_idx.

Parameters
UUser-defined unitary 2x2 matrix of templated type Mat2x2Type
qubit_idxIndex of qubit to apply gate upon

Definition at line 201 of file Simulator.hpp.

201  {
202  static_cast<DerivedType*>(this)->applyGateU(U, qubit_idx);
203  }

◆ applyGateX()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateX ( std::size_t  qubit_idx)
inline

Apply the Pauli X gate to the given qubit.

Parameters
qubit_idx

Definition at line 123 of file Simulator.hpp.

123  {
124  static_cast<DerivedType&>(*this).applyGateX(qubit_idx);
125  };

Referenced by QNLP::SimulatorGeneral< IntelSimulator >::encodeToRegister().

Here is the caller graph for this function:

◆ applyGateY()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateY ( std::size_t  qubit_idx)
inline

Apply the Pauli Y gate to the given qubit.

Parameters
qubit_idx

Definition at line 131 of file Simulator.hpp.

131  {
132  static_cast<DerivedType&>(*this).applyGateY(qubit_idx);
133  }

◆ applyGateZ()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyGateZ ( std::size_t  qubit_idx)
inline

Apply the Pauli Z gate to the given qubit.

Parameters
qubit_idx

Definition at line 139 of file Simulator.hpp.

139  {
140  static_cast<DerivedType&>(*this).applyGateZ(qubit_idx);
141  }

◆ applyHammingDistanceOverwrite()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyHammingDistanceOverwrite ( std::size_t  test_pattern,
const std::vector< std::size_t >  reg_mem,
const std::vector< std::size_t >  reg_auxiliary,
std::size_t  len_bin_pattern 
)
inline

Computes the relative Hamming distance between the test pattern and the pattern stored in each state of the superposition, overwriting the aux register pattern with the resulting bit differences.

Parameters
test_patternThe binary pattern used as the the basis for the Hamming Distance.
reg_memVector containing the indices of the register qubits that contain the training patterns.
reg_auxiliaryVector containing the indices of the register qubits which the first len_bin_pattern qubits will store the test_pattern.

Definition at line 609 of file Simulator.hpp.

612  {
613 
614  assert(reg_mem.size() < reg_auxiliary.size()-1);
615 
616  // Encode test pattern to auxiliary register
617  encodeToRegister(test_pattern, reg_auxiliary, len_bin_pattern);
618 
619  HammingDistance<DerivedType>::computeHammingDistanceOverwriteAux(static_cast<DerivedType&>(*this), reg_mem, reg_auxiliary);
620  }
void encodeToRegister(std::size_t target_pattern, const std::vector< std::size_t > target_register, std::size_t len_bin_pattern)
Encodes a defined binary pattern into a defined target register (initially in state |00....
Definition: Simulator.hpp:548
static void computeHammingDistanceOverwriteAux(SimulatorType &qSim, const std::vector< std::size_t > &reg_memory, const std::vector< std::size_t > &reg_auxiliary)
Computes Hamming Distance; Overwrites the pattern in reg_auxiliary to track bit differences from reg_...
Definition: hamming.hpp:102

◆ applyHammingDistanceRotY()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyHammingDistanceRotY ( std::size_t  test_pattern,
const std::vector< std::size_t >  reg_mem,
const std::vector< std::size_t >  reg_auxiliary,
std::size_t  len_bin_pattern 
)
inline

Computes the relative Hamming distance between the test pattern and the pattern stored in each state of the superposition, storing the result in the amplitude of the corresponding state.

Parameters
test_patternThe binary pattern used as the the basis for the Hamming Distance.
reg_memVector containing the indices of the register qubits that contain the training patterns.
reg_auxiliaryVector containing the indices of the register qubits which the first len_bin_pattern qubits will store the test_pattern.
len_bin_patternLength of the binary patterns

Definition at line 584 of file Simulator.hpp.

587  {
588 
589  assert(len_bin_pattern < reg_auxiliary.size()-1);
590 
591  // Encode test pattern to auxiliary register
592  encodeToRegister(test_pattern, reg_auxiliary, len_bin_pattern);
593 
594  HammingDistance<DerivedType> hamming_operator(len_bin_pattern);
595  hamming_operator.computeHammingDistanceRotY(static_cast<DerivedType&>(*this), reg_mem, reg_auxiliary, len_bin_pattern);
596 
597  // Un-encode test pattern from auxiliary register
598  encodeToRegister(test_pattern, reg_auxiliary, len_bin_pattern);
599  }
void encodeToRegister(std::size_t target_pattern, const std::vector< std::size_t > target_register, std::size_t len_bin_pattern)
Encodes a defined binary pattern into a defined target register (initially in state |00....
Definition: Simulator.hpp:548

◆ applyIQFT()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyIQFT ( std::size_t  minIdx,
std::size_t  maxIdx 
)
inline

Apply the inverse Quantum Fourier transform (IQFT) to the given register index range.

Parameters
minIdxLowest qubit index of the IQFT range
maxIdxHighest qubit index of the IQFT range

Definition at line 433 of file Simulator.hpp.

433  {
434  QFT<decltype(static_cast<DerivedType&>(*this))>::applyIQFT(static_cast<DerivedType&>(*this), minIdx, maxIdx);
435  }
void applyIQFT(std::size_t minIdx, std::size_t maxIdx)
Apply the inverse Quantum Fourier transform (IQFT) to the given register index range.
Definition: Simulator.hpp:433

Referenced by QNLP::SimulatorGeneral< IntelSimulator >::applyIQFT().

Here is the caller graph for this function:

◆ applyMeasurement()

template<class DerivedType>
bool QNLP::SimulatorGeneral< DerivedType >::applyMeasurement ( std::size_t  target,
bool  normalize = true 
)
inline

Apply measurement to a target qubit, randomly collapsing the qubit proportional to the amplitude and returns the collapsed value.

Returns
bool Value that qubit is randomly collapsed to
Parameters
targetThe index of the qubit being collapsed
normalizeOptional argument specifying whether amplitudes shoud be normalized (true) or not (false). Default value is true.

Definition at line 629 of file Simulator.hpp.

629  {
630  return static_cast<DerivedType*>(this)->applyMeasurement(target, normalize);
631  }

◆ applyMeasurementToRegister()

template<class DerivedType>
std::size_t QNLP::SimulatorGeneral< DerivedType >::applyMeasurementToRegister ( std::vector< std::size_t >  target_qubits,
bool  normalize = true 
)
inline

Apply measurement to a set of target qubits, randomly collapsing the qubits proportional to the amplitude and returns the bit string of the qubits in the order they are represented in the vector of indexes, in the form of an unsigned integer.

Returns
std::size_t Integer representing the binary string of the collapsed qubits, ordered by least significant digit corresponding to first qubit in target vector of indices
Parameters
target_qubitsVector of indices of qubits being collapsed
normalizeOptional argument specifying whether amplitudes shoud be normalized (true) or not (false). Default value is true.

Definition at line 640 of file Simulator.hpp.

640  {
641  // Store current state of training register in it's integer format
642  std::size_t val = 0;
643  for(int j = target_qubits.size() - 1; j > -1; j--){
644  val |= (static_cast<DerivedType*>(this)->applyMeasurement(target_qubits[j], normalize) << j);
645  }
646  return val;
647  }

◆ applyOraclePhase()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyOraclePhase ( std::size_t  bit_pattern,
const std::vector< std::size_t > &  ctrlIndices,
std::size_t  target 
)
inline

Apply oracle to match given binary index with linearly adjacent controls.

Parameters
bit_patternOracle pattern in binary
ctrlIndicesControl lines for oracle
targetTarget qubit index to apply Z gate upon

Definition at line 524 of file Simulator.hpp.

524  {
525  //applyOracleU<decltype(static_cast<DerivedType*>(this)->getGateZ())>(bit_pattern, ctrlIndices, target, static_cast<DerivedType*>(this)->getGateZ());
526  Oracle<DerivedType> oracle;
527  oracle.bitStringPhaseOracle(static_cast<DerivedType&>(*this), bit_pattern, ctrlIndices, target );
528  }

◆ applyOracleU() [1/2]

template<class DerivedType>
template<class Mat2x2Type >
void QNLP::SimulatorGeneral< DerivedType >::applyOracleU ( std::size_t  bit_pattern,
const std::vector< std::size_t > &  ctrlIndices,
std::size_t  target,
const Mat2x2Type &  U,
std::string  gateLabel 
)
inline

Apply oracle to match given binary index with non adjacent controls.

Parameters
bit_patternOracle state pattern (bitstring: 0-> |0>, 1-> |1>)
U2x2 unitary matrix to apply
ctrlIndicesControl indices for operation
targetTarget qubit index to apply U on
gateLabelLabel used to access required cached matrix for NCU

Definition at line 498 of file Simulator.hpp.

498  {
499  Oracle<DerivedType>::bitStringNCU(static_cast<DerivedType&>(*this), bit_pattern, ctrlIndices, target, U, gateLabel);
500  }
static void bitStringNCU(SimulatorType &s, std::size_t bitstring, const std::vector< std::size_t > &ctrl_indices, const std::size_t target, const Mat2x2Type &U, std::string gateLabel)
Takes bitstring as the binary pattern and indices as the qubits to operate upon. Applies the appropri...
Definition: oracle.hpp:54

◆ applyOracleU() [2/2]

template<class DerivedType>
template<class Mat2x2Type >
void QNLP::SimulatorGeneral< DerivedType >::applyOracleU ( std::size_t  bit_pattern,
const std::vector< std::size_t > &  ctrlIndices,
const std::vector< std::size_t > &  auxIndices,
std::size_t  target,
const Mat2x2Type &  U,
std::string  gateLabel 
)
inline

Apply oracle to match given binary index with non adjacent controls.

Parameters
bit_patternOracle state pattern (bitstring: 0-> |0>, 1-> |1>)
U2x2 unitary matrix to apply
ctrlIndicesControl indices for operation
auxIndicesAuxiliary indices for operation
targetTarget qubit index to apply U on
gateLabelLabel used to access required cached matrix for NCU

Definition at line 513 of file Simulator.hpp.

513  {
514  Oracle<DerivedType>::bitStringNCU(static_cast<DerivedType&>(*this), bit_pattern, ctrlIndices, auxIndices, target, U, gateLabel);
515  }
static void bitStringNCU(SimulatorType &s, std::size_t bitstring, const std::vector< std::size_t > &ctrl_indices, const std::size_t target, const Mat2x2Type &U, std::string gateLabel)
Takes bitstring as the binary pattern and indices as the qubits to operate upon. Applies the appropri...
Definition: oracle.hpp:54

◆ applyQFT()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::applyQFT ( std::size_t  minIdx,
std::size_t  maxIdx 
)
inline

Apply the forward Quantum Fourier transform (QFT) to the given register index range.

Parameters
minIdxLowest qubit index of the QFT range
maxIdxHighest qubit index of the QFT range

Definition at line 423 of file Simulator.hpp.

423  {
424  QFT<decltype(static_cast<DerivedType&>(*this))>::applyQFT(static_cast<DerivedType&>(*this), minIdx, maxIdx);
425  }
void applyQFT(std::size_t minIdx, std::size_t maxIdx)
Apply the forward Quantum Fourier transform (QFT) to the given register index range.
Definition: Simulator.hpp:423

Referenced by QNLP::SimulatorGeneral< IntelSimulator >::applyQFT().

Here is the caller graph for this function:

◆ collapseToBasisZ()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::collapseToBasisZ ( std::size_t  target,
bool  collapseValue 
)
inline

Apply measurement to a target qubit with respect to the Z-basis, collapsing to a specified value (0 or 1). Amplitudes are r-normalized afterwards.

Parameters
targetThe index of the qubit being collapsed
collapseValueThe value that the register will be collapsed to (either 0 ro 1).

Definition at line 671 of file Simulator.hpp.

671  {
672  static_cast<DerivedType*>(this)->collapseToBasisZ(target, collapseValue);
673  }

◆ encodeBinToSuperpos_unique()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::encodeBinToSuperpos_unique ( const std::vector< std::size_t > &  reg_memory,
const std::vector< std::size_t > &  reg_auxiliary,
const std::vector< std::size_t > &  bin_patterns,
const std::size_t  len_bin_pattern 
)
inline

Encode inputted binary strings to the memory register specified as a superposition of states. Note that this implementation does not allow for multiple instances of the same input pattern but allows for 0 to be encoded.

Parameters
reg_memorystd::vector of unsigned integers containing the indices of the circuit's memory register
reg_auxiliarystd::vector of unsigned integers type containing the indices of the circuit's auxiliary register
bin_patternsstd::vector of unsigned integers representing the binary patterns to encode
len_bin_patternThe length of the binary patterns being encoded

Definition at line 567 of file Simulator.hpp.

570  {
571 
572  EncodeBinIntoSuperpos<DerivedType> encoder(bin_patterns.size(), len_bin_pattern);
573  encoder.encodeBinInToSuperpos_unique(static_cast<DerivedType&>(*this), reg_memory, reg_auxiliary, bin_patterns);
574  }

◆ encodeToRegister()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::encodeToRegister ( std::size_t  target_pattern,
const std::vector< std::size_t >  target_register,
std::size_t  len_bin_pattern 
)
inline

Encodes a defined binary pattern into a defined target register (initially in state |00...0>) of all quantum states in the superposition.

Parameters
target_patternThe binary pattern that is to be encoded
target_registerVector containing the indices of the register qubits that the pattern is to be encoded into (beginning at least significant digit). Note, the target register is expected to be in the state consisting of all 0's before the encoding.
Lengthof the binary pattern to be encoded

Definition at line 548 of file Simulator.hpp.

550  {
551 
552  for(std::size_t i = 0; i < len_bin_pattern; i++){
553  if(IS_SET(target_pattern,i)){
554  applyGateX(target_register[i]);
555  }
556  }
557  }
#define IS_SET(byte, bit)
Definition: Simulator.hpp:58
void applyGateX(std::size_t qubit_idx)
Apply the Pauli X gate to the given qubit.
Definition: Simulator.hpp:123

Referenced by QNLP::SimulatorGeneral< IntelSimulator >::applyHammingDistanceOverwrite(), and QNLP::SimulatorGeneral< IntelSimulator >::applyHammingDistanceRotY().

Here is the caller graph for this function:

◆ getGateH()

template<class DerivedType>
decltype(auto) QNLP::SimulatorGeneral< DerivedType >::getGateH ( )
inline

Get the Hadamard gate; must be overloaded with appropriate return type.

Returns
GateType Templated return type of Hadamard gate

Definition at line 238 of file Simulator.hpp.

238  {
239  return static_cast<DerivedType*>(this)->getGateH();
240  }

◆ getGateI()

template<class DerivedType>
decltype(auto) QNLP::SimulatorGeneral< DerivedType >::getGateI ( )
inline

Get the Identity; must be overloaded with appropriate return type.

Returns
GateType Templated return type of Identity gate

Definition at line 231 of file Simulator.hpp.

231  {
232  return static_cast<DerivedType*>(this)->getGateI();
233  }

◆ getGateX()

template<class DerivedType>
decltype(auto) QNLP::SimulatorGeneral< DerivedType >::getGateX ( )
inline

Get the Pauli-X gate; returns templated type GateType.

Returns
GateType Templated return type of Pauli-X gate

Definition at line 209 of file Simulator.hpp.

209  {
210  return static_cast<DerivedType&>(*this).getGateX();
211  }

◆ getGateY()

template<class DerivedType>
decltype(auto) QNLP::SimulatorGeneral< DerivedType >::getGateY ( )
inline

Get the Pauli-Y gate; must be overloaded with appropriate return type.

Returns
GateType Templated return type of Pauli-Y gate

Definition at line 217 of file Simulator.hpp.

217  {
218  return static_cast<DerivedType&>(*this).getGateY();
219  }

◆ getGateZ()

template<class DerivedType>
decltype(auto) QNLP::SimulatorGeneral< DerivedType >::getGateZ ( )
inline

Get the Pauli-Z gate; must be overloaded with appropriate return type.

Returns
GateType Templated return type of Pauli-Z gate

Definition at line 224 of file Simulator.hpp.

224  {
225  return static_cast<DerivedType&>(*this).getGateZ();
226  }

◆ getNumQubits()

template<class DerivedType>
std::size_t QNLP::SimulatorGeneral< DerivedType >::getNumQubits ( )
inline

Get the number of Qubits.

Returns
std::size_t Number of qubits in register

Definition at line 413 of file Simulator.hpp.

413  {
414  return static_cast<DerivedType*>(this)->getNumQubits();
415  }

Referenced by QNLP::SimulatorGeneral< IntelSimulator >::applyGateCSwap(), and TEST_CASE().

Here is the caller graph for this function:

◆ getQubitRegister()

template<class DerivedType>
decltype(auto) QNLP::SimulatorGeneral< DerivedType >::getQubitRegister ( )
inline

Get the underlying qubit register object.

Returns
decltype(auto) the underlying bound qubit register

Definition at line 404 of file Simulator.hpp.

404  {
405  return static_cast<DerivedType*>(this)->getQubitRegister();
406  }

◆ groupQubits()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::groupQubits ( const std::vector< std::size_t >  reg_auxiliary,
bool  lsb = true 
)
inline

Group all set qubits to MSB in register (ie |010100> -> |000011>)

Parameters
reg_memThe indices of the qubits to perform operation upon
reg_auxiliaryThe auxiliary control qubit register set to |......10> at the beginning, and guaranteed to be set the same at end.
lsbShifts to LSB position in register if true; MSB otherwise.

Definition at line 656 of file Simulator.hpp.

656  {
657  assert( reg_auxiliary.size() >= 2);
658 
659  const std::vector<std::size_t> reg_ctrl ( reg_auxiliary.end()-2, reg_auxiliary.end() );
660  const std::vector<std::size_t> sub_reg (reg_auxiliary.begin(), reg_auxiliary.end()-2 ) ;
661 
662  BitGroup<DerivedType>::bit_group(static_cast<DerivedType&>(*this), sub_reg, reg_ctrl, lsb);
663  }
static void bit_group(SimulatorType &qSim, const std::vector< std::size_t > &qreg_idx, const std::vector< std::size_t > &qaux_idx, bool lsb=true)
Swaps all qubits in register indices given by qreg_idx to their right-most positions....
Definition: bit_group.hpp:114

◆ initCaches()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::initCaches ( )
inline

Initialise caches used in NCU operation.

Definition at line 687 of file Simulator.hpp.

687  {
688  #if defined(__INTEL_COMPILER) || defined(__INTEL_LLVM_COMPILER)
689  std::experimental::any_cast<NCU<DerivedType>&>(sim_ncu).initialiseMaps(static_cast<DerivedType&>(*this), 16);
690  #else
691  std::any_cast<NCU<DerivedType>&>(sim_ncu).initialiseMaps(static_cast<DerivedType&>(*this), 16);
692  #endif
693  }

◆ initRegister()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::initRegister ( )
inline

(Re)Initialise the underlying register of the encapsulated simulator to well-defined state (|0....0>)

Definition at line 679 of file Simulator.hpp.

679  {
680  static_cast<DerivedType&>(*this).initRegister();
681  }

◆ InvertRegister()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::InvertRegister ( const unsigned int  minIdx,
const unsigned int  maxIdx 
)
inline

Invert the register about the given indides: 0,1,2...n-1,n -> n,n-1,...,1,0.

Parameters
minIdxThe lower index of the inversion
maxIdxThe upper index of the inversion

Definition at line 776 of file Simulator.hpp.

776  {
777  unsigned int range2 = ((maxIdx - minIdx)%2 == 1) ? (maxIdx - minIdx)/2 +1 : (maxIdx - minIdx)/2;
778  for(unsigned int idx = 0; idx < range2; idx++){
779  applyGateSwap(minIdx+idx, maxIdx-idx);
780  }
781  }
void applyGateSwap(std::size_t qubit_idx0, std::size_t qubit_idx1)
Swap the qubits at the given indices.
Definition: Simulator.hpp:304

◆ matrixSqrt()

template<class DerivedType>
template<class Mat2x2Type >
Mat2x2Type QNLP::SimulatorGeneral< DerivedType >::matrixSqrt ( const Mat2x2Type &  U)
inline

Calculates the unitary matrix square root (U == VV, where V is returned)

Template Parameters
Mat2x2TypeMatrix type
Parameters
UUnitary matrix to be rooted
Returns
openqu::TinyMatrix<Type, 2, 2, 32> V such that VV == U

Definition at line 729 of file Simulator.hpp.

729  {
730  Mat2x2Type V(U);
731  std::complex<double> delta = U(0,0)*U(1,1) - U(0,1)*U(1,0);
732  std::complex<double> tau = U(0,0) + U(1,1);
733  std::complex<double> s = sqrt(delta);
734  std::complex<double> t = sqrt(tau + 2.0*s);
735 
736  //must be a way to vectorise these; TinyMatrix have a scale/shift option?
737  V(0,0) += s;
738  V(1,1) += s;
739  std::complex<double> scale_factor(1.,0.);
740  scale_factor/=t;
741  V(0,0) *= scale_factor; //(std::complex<double>(1.,0.)/t);
742  V(0,1) *= scale_factor; //(1/t);
743  V(1,0) *= scale_factor; //(1/t);
744  V(1,1) *= scale_factor; //(1/t);
745 
746  return V;
747  }

◆ PrintStates()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::PrintStates ( std::string  x,
std::vector< std::size_t >  qubits = {} 
)
inline

Prints the string x and then for each state of the specified qubits in the superposition, prints each its amplitude, followed by state and then by the probability of that state. Note that this state observation method is not a permitted quantum operation, however it is provided for convenience and debugging/testing.

Parameters
xString to be printed to stdout
qubitsIndices of qubits in register to be printed

Definition at line 717 of file Simulator.hpp.

717  {}){
718  static_cast<DerivedType*>(this)->PrintStates(x, qubits);
719  }

◆ subReg()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::subReg ( std::size_t  r0_minIdx,
std::size_t  r0_maxIdx,
std::size_t  r1_minIdx,
std::size_t  r1_maxIdx 
)
inline

Applies |r1>|r2> -> |r1>|r1-r2>

Definition at line 448 of file Simulator.hpp.

448  {
449  Arithmetic<decltype(static_cast<DerivedType&>(*this))>::sub_reg(static_cast<DerivedType&>(*this), r0_minIdx, r0_maxIdx, r1_minIdx, r1_maxIdx);
450  }

◆ sumReg()

template<class DerivedType>
void QNLP::SimulatorGeneral< DerivedType >::sumReg ( std::size_t  r0_minIdx,
std::size_t  r0_maxIdx,
std::size_t  r1_minIdx,
std::size_t  r1_maxIdx 
)
inline

Applies |r1>|r2> -> |r1>|r1+r2>

Definition at line 441 of file Simulator.hpp.

441  {
442  Arithmetic<decltype(static_cast<DerivedType&>(*this))>::sum_reg(static_cast<DerivedType&>(*this), r0_minIdx, r0_maxIdx, r1_minIdx, r1_maxIdx);
443  }

Field Documentation

◆ DerivedType

template<class DerivedType>
friend QNLP::SimulatorGeneral< DerivedType >::DerivedType
private

Definition at line 91 of file Simulator.hpp.

◆ sim_ncu


The documentation for this class was generated from the following file: