Skip to content

Commit

Permalink
Added SingleTargetGate virtual class
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-deaconescu committed Apr 20, 2023
1 parent 67c406a commit ac04d02
Show file tree
Hide file tree
Showing 15 changed files with 442 additions and 69 deletions.
16 changes: 7 additions & 9 deletions examples/shors_algorithm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

#include <numbers>

using namespace QPP;

/// @brief U gate for N = 15
Circuit<double>::CircuitGate UGate(const size_t& a, const unsigned long& power){
QPP::Circuit<double>::CircuitGate UGate(const size_t& a, const unsigned long& power){
// Controlled multiplication by a mod 15

// Check if a is not coprime to N (15)
if(a != 2 && a != 4 && a != 7 && a != 8 && a != 11 && a != 13){
throw std::invalid_argument("a must be coprime to N (15)");
}
auto circuit = Circuit<double>(std::make_shared<ProbabilityEngine<double>>(), 4);
auto circuit = Circuit<double>(std::make_shared<QPP::ProbabilityEngine<double>>(), 4);
for (size_t iteration = 0; iteration < power; iteration++) {
if(a == 2 || a == 13){
circuit.addSwapGate(2, 3);
Expand All @@ -41,8 +39,8 @@ Circuit<double>::CircuitGate UGate(const size_t& a, const unsigned long& power){
}

/// @brief Quantum Fourier Transform
Circuit<double> CQFT(const std::shared_ptr<ProbabilityEngine<double>>& probabilityEngine, const size_t &n) {
Circuit<double> circuit(probabilityEngine, n, n);
QPP::Circuit<double> CQFT(const std::shared_ptr<QPP::ProbabilityEngine<double>>& probabilityEngine, const size_t &n) {
QPP::Circuit<double> circuit(probabilityEngine, n, n);

// Apply inverse quantum Fourier transform
for (size_t index = 0; index < n / 2; index++) {
Expand All @@ -64,12 +62,12 @@ Circuit<double> CQFT(const std::shared_ptr<ProbabilityEngine<double>>& probabili
/// f(x) = a^x mod N
/// where a and N are integers and a is coprime to N
/// The period of the function is the smallest positive integer r such that f(x) = f(x + r) = 1
Circuit<double>::CompoundResult shorsAlgorithm(unsigned long a, unsigned long countingQubits, unsigned long repetitions = 1000) {
inline QPP::Circuit<double>::CompoundResult shorsAlgorithm(unsigned long a, unsigned long countingQubits, unsigned long repetitions = 1000) {

// Create a Quantum Circuit with N counting qubits plus 4 qubits for U to act on.
const std::shared_ptr<ProbabilityEngine<double>> &probabilityEngine = std::make_shared<ProbabilityEngine<double>>();
const std::shared_ptr<QPP::ProbabilityEngine<double>> &probabilityEngine = std::make_shared<QPP::ProbabilityEngine<double>>();

auto circuit = Circuit<double>(probabilityEngine, countingQubits + 4, countingQubits);
auto circuit = QPP::Circuit<double>(probabilityEngine, countingQubits + 4, countingQubits);

// Initialize counting qubits to |+>
for (size_t i = 0; i < countingQubits; i++) {
Expand Down
47 changes: 47 additions & 0 deletions examples/teleportation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <memory>

#include "../include/circuit.hpp"

inline void runQuantumTeleportationExample(){
// Create a Quantum Circuit with 3 qubits and 2 classical bits
const std::shared_ptr<QPP::ProbabilityEngine<double>> &probabilityEngine = std::make_shared<QPP::ProbabilityEngine<double>>();
auto circuit = QPP::Circuit<double>(probabilityEngine, 3, 2);

std::cout << "Quantum Teleportation Example" << std::endl << std::endl;

std::cout << "First step: Initialize the qubit we want to teleport to a random state" << std::endl << std::endl;

// Initialize the first qubit in a random state
circuit.addInitGate(0, QPP::Qubit<double>::State::Random(probabilityEngine));

std::cout << circuit << std::endl << std::endl;

std::cout << "Second step: Create an entangled pair of qubits" << std::endl << std::endl;

circuit.addHadamardGate(1);
circuit.addCXGate(1, 2);

std::cout << circuit << std::endl << std::endl;

std::cout << "Third step: Entangle the teleportation qubit to the second qubit" << std::endl << std::endl;

circuit.addCXGate(0, 1);
circuit.addHadamardGate(0);

std::cout << circuit << std::endl << std::endl;

std::cout << "Fourth step: Measure the first two qubits" << std::endl << std::endl;

// Measure the first two qubits
circuit.addMeasureGate({{0, 0}, {1, 1}});

std::cout << circuit << std::endl << std::endl;

std::cout << "Fifth step: Apply the appropriate gates based on the measurement results" << std::endl << std::endl;

// Apply the appropriate gates based on the measurement results
circuit.addGate(std::make_unique<QPP::Circuit<double>::XGate>(2)->makeControlled(1, true));
circuit.addGate(std::make_unique<QPP::Circuit<double>::ZGate>(2)->makeControlled(0, true));

std::cout << circuit << std::endl << std::endl;
}
Loading

0 comments on commit ac04d02

Please sign in to comment.