QNLP
v1.0
|
CRTP defined class for simulator implementations. More...
#include <Simulator.hpp>
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 > ®_memory, const std::vector< std::size_t > ®_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 |
CRTP defined class for simulator implementations.
DerivedType | CRTP derived class simulator type |
Definition at line 69 of file Simulator.hpp.
|
inlineprivate |
|
inlinevirtual |
|
inline |
Adds a matrix to the cache, assigning it to a defined label. This is used in the caching for the NCU operation.
Mat2x2Type | Matrix type to be cached |
gateLabel | Label assigned to the matrix being cached |
U | Matrix to be cached |
Definition at line 703 of file Simulator.hpp.
|
inlinestatic |
Function to calculate the adjoint of an input matrix.
Mat2x2Type | Matrix type |
U | Unitary matrix to be adjointed |
Definition at line 757 of file Simulator.hpp.
|
inline |
Apply diffusion operator on marked state.
ctrlIndices | Vector of control line indices |
target | Target qubit index to apply Ctrl-Z upon. |
Definition at line 536 of file Simulator.hpp.
|
inline |
Controlled controlled NOT (CCNOT, CCX) gate.
ctrl_qubit0 | Control qubit 0 |
ctrl_qubit1 | Control qubit 1 |
target_qubit | Target qubit |
Definition at line 346 of file Simulator.hpp.
|
inline |
Apply Controlled Hadamard on target qubit.
control | Qubit index acting as control |
target | Qubit index acting as target |
Definition at line 294 of file Simulator.hpp.
|
inline |
Perform controlled phase shift gate.
angle | Angle of phase shift in rads |
control | Index of control qubit |
target | Index of target qubit |
Definition at line 335 of file Simulator.hpp.
|
inline |
Apply the given Controlled Rotation about X-axis to the given qubit.
ctrl_qubit | Control qubit |
qubit_idx | Index of qubit to rotate about X-axis |
angle_rad | Rotation angle |
Definition at line 373 of file Simulator.hpp.
|
inline |
Apply the given Controlled Rotation about Y-axis to the given qubit.
ctrl_qubit | Control qubit |
qubit_idx | Index of qubit to rotate about Y-axis |
angle_rad | Rotation angle |
Definition at line 384 of file Simulator.hpp.
|
inline |
Apply the given Controlled Rotation about Z-axis to the given qubit.
ctrl_qubit | Control qubit |
qubit_idx | Index of qubit to rotate about Z-axis |
angle_rad | Rotation angle |
Definition at line 395 of file Simulator.hpp.
|
inline |
Controlled SWAP gate.
ctrl_qubit | Control qubit |
qubit_swap0 | Swap qubit 0 |
qubit_swap1 | Swap qubit 1 |
Definition at line 357 of file Simulator.hpp.
|
inline |
Apply the given controlled unitary gate on target qubit.
U | User-defined arbitrary 2x2 unitary gate (matrix) |
control | Qubit index acting as control |
target | Qubit index acting as target |
Definition at line 254 of file Simulator.hpp.
|
inline |
Apply Controlled Pauli-X (CNOT) on target qubit.
control | Qubit index acting as control |
target | Qubit index acting as target |
Definition at line 264 of file Simulator.hpp.
|
inline |
Apply Controlled Pauli-Y on target qubit.
control | Qubit index acting as control |
target | Qubit index acting as target |
Definition at line 274 of file Simulator.hpp.
|
inline |
Apply Controlled Pauli-Z on target qubit.
control | Qubit index acting as control |
target | Qubit index acting as target |
Definition at line 284 of file Simulator.hpp.
|
inline |
Apply the Hadamard gate to the given qubit.
qubit_idx |
Definition at line 155 of file Simulator.hpp.
|
inline |
Apply the Identity gate to the given qubit.
qubit_idx |
Definition at line 147 of file Simulator.hpp.
|
inline |
Apply n-control unitary gate to the given qubit target.
Mat2x2Type | 2x2 Matrix type of unitary gate in the format expected by the derived simulator object; decltype(simulator.getGateX()) can be used in template |
U | 2x2 unitary matrix |
ctrlIndices | Vector of the control lines for NCU operation |
target | Target qubit index to apply nCU |
label | Gate label string (U, X, Y, etc.) |
Definition at line 462 of file Simulator.hpp.
|
inline |
Apply n-control sigma_x gate to the given qubit target, using auxiliary qubits for 5CX optimisation.
Mat2x2Type | 2x2 Matrix type of unitary gate in the format expected by the derived simulator object; decltype(simulator.getGateX()) can be used in template |
U | 2x2 unitary matrix |
minIdx | Lowest index of the control lines expected for nCU |
maxIdx | Highest index of the control lines expected for the nCU |
target | Target qubit index to apply nCU |
Definition at line 480 of file Simulator.hpp.
|
inline |
Apply phase shift to given Qubit; [[1 0] [0 exp(i*angle)]].
angle | Angle of phase shift in rads |
qubit_idx | Qubit index to perform phase shift upon |
Definition at line 324 of file Simulator.hpp.
|
inline |
Apply the given Rotation about X-axis to the given qubit.
qubit_idx | Index of qubit to rotate about X-axis |
angle_rad | Rotation angle |
Definition at line 172 of file Simulator.hpp.
|
inline |
Apply the given Rotation about Y-axis to the given qubit.
qubit_idx | Index of qubit to rotate about Y-axis |
angle_rad | Rotation angle |
Definition at line 181 of file Simulator.hpp.
|
inline |
Apply the given Rotation about Z-axis to the given qubit.
qubit_idx | Index of qubit to rotate about Z-axis |
angle_rad | Rotation angle |
Definition at line 190 of file Simulator.hpp.
|
inline |
Performs Sqrt SWAP gate between two given qubits (half way SWAP)
qubit_idx0 | Qubit index 0 |
qubit_idx1 | Qubit index 1 |
Definition at line 314 of file Simulator.hpp.
|
inline |
Apply the Sqrt{Pauli X} gate to the given qubit.
qubit_idx |
Definition at line 163 of file Simulator.hpp.
|
inline |
Swap the qubits at the given indices.
qubit_idx0 | Index of qubit 0 to swap &(0 -> 1) |
qubit_idx1 | Index of qubit 1 to swap &(1 -> 0) |
Definition at line 304 of file Simulator.hpp.
Referenced by QNLP::SimulatorGeneral< IntelSimulator >::InvertRegister().
|
inline |
Apply arbitrary user-defined unitary gate to qubit at qubit_idx.
U | User-defined unitary 2x2 matrix of templated type Mat2x2Type |
qubit_idx | Index of qubit to apply gate upon |
Definition at line 201 of file Simulator.hpp.
|
inline |
Apply the Pauli X gate to the given qubit.
qubit_idx |
Definition at line 123 of file Simulator.hpp.
Referenced by QNLP::SimulatorGeneral< IntelSimulator >::encodeToRegister().
|
inline |
Apply the Pauli Y gate to the given qubit.
qubit_idx |
Definition at line 131 of file Simulator.hpp.
|
inline |
Apply the Pauli Z gate to the given qubit.
qubit_idx |
Definition at line 139 of file Simulator.hpp.
|
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.
test_pattern | The binary pattern used as the the basis for the Hamming Distance. |
reg_mem | Vector containing the indices of the register qubits that contain the training patterns. |
reg_auxiliary | Vector 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.
|
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.
test_pattern | The binary pattern used as the the basis for the Hamming Distance. |
reg_mem | Vector containing the indices of the register qubits that contain the training patterns. |
reg_auxiliary | Vector containing the indices of the register qubits which the first len_bin_pattern qubits will store the test_pattern. |
len_bin_pattern | Length of the binary patterns |
Definition at line 584 of file Simulator.hpp.
|
inline |
Apply the inverse Quantum Fourier transform (IQFT) to the given register index range.
minIdx | Lowest qubit index of the IQFT range |
maxIdx | Highest qubit index of the IQFT range |
Definition at line 433 of file Simulator.hpp.
Referenced by QNLP::SimulatorGeneral< IntelSimulator >::applyIQFT().
|
inline |
Apply measurement to a target qubit, randomly collapsing the qubit proportional to the amplitude and returns the collapsed value.
target | The index of the qubit being collapsed |
normalize | Optional argument specifying whether amplitudes shoud be normalized (true) or not (false). Default value is true. |
Definition at line 629 of file Simulator.hpp.
|
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.
target_qubits | Vector of indices of qubits being collapsed |
normalize | Optional argument specifying whether amplitudes shoud be normalized (true) or not (false). Default value is true. |
Definition at line 640 of file Simulator.hpp.
|
inline |
Apply oracle to match given binary index with linearly adjacent controls.
bit_pattern | Oracle pattern in binary |
ctrlIndices | Control lines for oracle |
target | Target qubit index to apply Z gate upon |
Definition at line 524 of file Simulator.hpp.
|
inline |
Apply oracle to match given binary index with non adjacent controls.
bit_pattern | Oracle state pattern (bitstring: 0-> |0>, 1-> |1>) |
U | 2x2 unitary matrix to apply |
ctrlIndices | Control indices for operation |
target | Target qubit index to apply U on |
gateLabel | Label used to access required cached matrix for NCU |
Definition at line 498 of file Simulator.hpp.
|
inline |
Apply oracle to match given binary index with non adjacent controls.
bit_pattern | Oracle state pattern (bitstring: 0-> |0>, 1-> |1>) |
U | 2x2 unitary matrix to apply |
ctrlIndices | Control indices for operation |
auxIndices | Auxiliary indices for operation |
target | Target qubit index to apply U on |
gateLabel | Label used to access required cached matrix for NCU |
Definition at line 513 of file Simulator.hpp.
|
inline |
Apply the forward Quantum Fourier transform (QFT) to the given register index range.
Definition at line 423 of file Simulator.hpp.
Referenced by QNLP::SimulatorGeneral< IntelSimulator >::applyQFT().
|
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.
target | The index of the qubit being collapsed |
collapseValue | The value that the register will be collapsed to (either 0 ro 1). |
Definition at line 671 of file Simulator.hpp.
|
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.
reg_memory | std::vector of unsigned integers containing the indices of the circuit's memory register |
reg_auxiliary | std::vector of unsigned integers type containing the indices of the circuit's auxiliary register |
bin_patterns | std::vector of unsigned integers representing the binary patterns to encode |
len_bin_pattern | The length of the binary patterns being encoded |
Definition at line 567 of file Simulator.hpp.
|
inline |
Encodes a defined binary pattern into a defined target register (initially in state |00...0>) of all quantum states in the superposition.
target_pattern | The binary pattern that is to be encoded |
target_register | Vector 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. |
Length | of the binary pattern to be encoded |
Definition at line 548 of file Simulator.hpp.
Referenced by QNLP::SimulatorGeneral< IntelSimulator >::applyHammingDistanceOverwrite(), and QNLP::SimulatorGeneral< IntelSimulator >::applyHammingDistanceRotY().
|
inline |
Get the Hadamard gate; must be overloaded with appropriate return type.
Definition at line 238 of file Simulator.hpp.
|
inline |
Get the Identity; must be overloaded with appropriate return type.
Definition at line 231 of file Simulator.hpp.
|
inline |
Get the Pauli-X gate; returns templated type GateType.
Definition at line 209 of file Simulator.hpp.
|
inline |
Get the Pauli-Y gate; must be overloaded with appropriate return type.
Definition at line 217 of file Simulator.hpp.
|
inline |
Get the Pauli-Z gate; must be overloaded with appropriate return type.
Definition at line 224 of file Simulator.hpp.
|
inline |
Get the number of Qubits.
Definition at line 413 of file Simulator.hpp.
Referenced by QNLP::SimulatorGeneral< IntelSimulator >::applyGateCSwap(), and TEST_CASE().
|
inline |
Get the underlying qubit register object.
Definition at line 404 of file Simulator.hpp.
|
inline |
Group all set qubits to MSB in register (ie |010100> -> |000011>)
reg_mem | The indices of the qubits to perform operation upon |
reg_auxiliary | The auxiliary control qubit register set to |......10> at the beginning, and guaranteed to be set the same at end. |
lsb | Shifts to LSB position in register if true; MSB otherwise. |
Definition at line 656 of file Simulator.hpp.
|
inline |
Initialise caches used in NCU operation.
Definition at line 687 of file Simulator.hpp.
|
inline |
(Re)Initialise the underlying register of the encapsulated simulator to well-defined state (|0....0>)
Definition at line 679 of file Simulator.hpp.
|
inline |
Invert the register about the given indides: 0,1,2...n-1,n -> n,n-1,...,1,0.
Definition at line 776 of file Simulator.hpp.
|
inline |
Calculates the unitary matrix square root (U == VV, where V is returned)
Mat2x2Type | Matrix type |
U | Unitary matrix to be rooted |
Definition at line 729 of file Simulator.hpp.
|
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.
x | String to be printed to stdout |
qubits | Indices of qubits in register to be printed |
Definition at line 717 of file Simulator.hpp.
|
inline |
Applies |r1>|r2> -> |r1>|r1-r2>
Definition at line 448 of file Simulator.hpp.
|
inline |
Applies |r1>|r2> -> |r1>|r1+r2>
Definition at line 441 of file Simulator.hpp.
|
private |
Definition at line 91 of file Simulator.hpp.
|
protected |