-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Vlad Gheorghiu <[email protected]>
- Loading branch information
Showing
9 changed files
with
131 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Conditional IF quantum circuit simulator | ||
// Source: ./examples/circuits/conditional_if.cpp | ||
|
||
#include <iostream> | ||
|
||
#include "qpp/qpp.hpp" | ||
|
||
int main() { | ||
using namespace qpp; | ||
|
||
std::cout << ">> Conditional IF quantum circuit simulator\n\n"; | ||
|
||
// quantum circuit with 2 qubits and 2 classical bits | ||
QCircuit qc{3, 3}; | ||
// prepare the first qubit in the |+> state | ||
qc.gate(gt.H, 0); | ||
// measure the first qubit non-destructively | ||
qc.measure(0, 0, false); | ||
|
||
// define a boolean predicate of the required form std::vector<idx> -> bool | ||
auto pred = [](std::vector<idx> dits) { | ||
// returns true when the first dit is 1 at runtime (when run by a | ||
// quantum engine); in our case, this corresponds to the result of the | ||
// measurement result of the first qubit | ||
return dits[0] == 1; | ||
}; | ||
// conditional IF statement | ||
// flips the second qubit when the predicate above was true, otherwise | ||
// flips the third qubit | ||
qc.cond_if(pred); | ||
// curly braces are optional, used to force code indenting | ||
{ | ||
qc.gate(gt.X, 1); // the final state will be |110> | ||
} | ||
qc.cond_else(); | ||
{ | ||
qc.gate(gt.X, 2); // the final state will be |001> | ||
} | ||
qc.cond_end(); | ||
|
||
// measure the second and third qubits non-destructively | ||
qc.measure(1, 1, false); | ||
qc.measure(2, 2, false); | ||
|
||
// display the quantum circuit and its corresponding resources | ||
std::cout << qc << "\n\n" << qc.get_resources() << "\n\n"; | ||
|
||
// initialize the quantum engine with the circuit | ||
QEngine engine{qc}; | ||
|
||
// execute the entire circuit once | ||
engine.execute(); | ||
|
||
// display the measurement statistics | ||
std::cout << engine << "\n\n"; | ||
// display the output quantum state | ||
std::cout << ">> Final state:\n"; | ||
std::cout << disp(dirac(engine.get_state())) << '\n'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Conditional WHILE quantum circuit simulator | ||
// Source: ./examples/circuits/conditional_while.cpp | ||
|
||
#include <iostream> | ||
|
||
#include "qpp/qpp.hpp" | ||
|
||
int main() { | ||
using namespace qpp; | ||
|
||
std::cout << ">> Conditional WHILE quantum circuit simulator\n\n"; | ||
|
||
// quantum circuit with 2 qubits and 2 classical bits | ||
QCircuit qc{3, 3}; | ||
|
||
// define a boolean predicate of the required form std::vector<idx> -> bool | ||
auto pred = [](std::vector<idx> dits) { | ||
// returns true as long as the first two classical dits ARE NOT 1, 1 | ||
return !(dits[0] == 1 && dits[1] == 1); | ||
}; | ||
|
||
// conditional WHILE statement | ||
// keep preparing the first measuring the first 2 qubits non-destructively | ||
// until we obtain the result 1, 1 | ||
qc.cond_while(pred); | ||
// curly braces are optional, used to force code indenting | ||
{ | ||
qc.reset({0, 1}); // resets the first two qubits to the |00> state | ||
qc.gate_fan(gt.H, {0, 1}); // next, prepare them in the |++> state | ||
qc.measure({0, 1}, 0, false); // finally, measure them non-destructively | ||
} // keep repeating until both measurement results are 1, 1 | ||
qc.cond_end(); | ||
|
||
// the WHILE statement finished, flip the state of the third qubit | ||
qc.gate(gt.X, 2); // the final state will be |111> | ||
qc.measure(2, 2); // measure the third qubit non-destructively | ||
|
||
// display the quantum circuit and its corresponding resources | ||
std::cout << qc << "\n\n" << qc.get_resources() << "\n\n"; | ||
|
||
// initialize the quantum engine with the circuit | ||
QEngine engine{qc}; | ||
|
||
// execute the entire circuit once | ||
engine.execute(); | ||
|
||
// display the measurement statistics | ||
std::cout << engine << "\n\n"; | ||
// display the output quantum state | ||
std::cout << ">> Final state:\n"; | ||
std::cout << disp(dirac(engine.get_state())) << '\n'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters