Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CCNOT example #175

Merged
merged 2 commits into from
May 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions examples/toffoli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Toffoli gate simulation
// Source: ./examples/circuits/toffoli.cpp

#include <iostream>

#include "qpp/qpp.h"

int main() {
using namespace qpp;

std::cout << ">> Toffoli gate simulation\n\n";

ket psi_in = randket(8);
std::cout << ">> Input state:\n";
std::cout << disp(dirac(psi_in)) << "\n\n";

/**
* Toffoli gate (control control not)
*
* ---+---
* |
* ---+---
* |
* ---X---
*/
ket result = apply(psi_in, gt.TOF, {0, 1, 2});
std::cout << ">> Toffoli gate output state:\n";
std::cout << disp(dirac(result)) << "\n\n";

/**
* Toffoli with T and CNOT
*
* -------------+-------------+-----+---T---+--
* | | | |
* -----+-------------+----------T--X--T_d--X--
* | | | |
* --H--X--T_d--X--T--X--T_d--X--T--H----------
*/
result = apply(psi_in, gt.H, {2});
result = applyCTRL(result, gt.X, {1}, {2});
result = apply(result, adjoint(gt.T), {2});
result = applyCTRL(result, gt.X, {0}, {2});
result = apply(result, gt.T, {2});
result = applyCTRL(result, gt.X, {1}, {2});
result = apply(result, adjoint(gt.T), {2});
result = applyCTRL(result, gt.X, {0}, {2});
result = apply(result, gt.T, {1});
result = apply(result, gt.T, {2});
result = applyCTRL(result, gt.X, {0}, {1});
result = apply(result, gt.T, {0});
result = apply(result, adjoint(gt.T), {1});
result = apply(result, gt.H, {2});
result = applyCTRL(result, gt.X, {0}, {1});
std::cout << ">> Toffoli with T and CNOT output state:\n";
std::cout << disp(dirac(result)) << "\n\n";

/**
* Sleator weinfurter construction
* V * V = X
*
* -----+-------+---+---
* | | |
* --+--X---+---X-------
* | | |
* --V-----V_d------V---
*/
cmat sqrtx{cmat::Zero(2, 2)};
sqrtx << 0.5 + 0.5 * 1_i, 0.5 - 0.5 * 1_i, 0.5 - 0.5 * 1_i, 0.5 + 0.5 * 1_i;
result = applyCTRL(psi_in, sqrtx, {1}, {2});
result = applyCTRL(result, gt.X, {0}, {1});
result = applyCTRL(result, adjoint(sqrtx), {1}, {2});
result = applyCTRL(result, gt.X, {0}, {1});
result = applyCTRL(result, sqrtx, {0}, {2});
std::cout << ">> sleator weinfurter construction output state:\n";
enum-class marked this conversation as resolved.
Show resolved Hide resolved
std::cout << disp(dirac(result)) << "\n\n";
}