Skip to content

Commit

Permalink
Added conditional_if/while examples
Browse files Browse the repository at this point in the history
Signed-off-by: Vlad Gheorghiu <[email protected]>
  • Loading branch information
vsoftco committed Jan 6, 2025
1 parent 8ba8d32 commit f881960
Show file tree
Hide file tree
Showing 9 changed files with 131 additions and 9 deletions.
18 changes: 15 additions & 3 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@
- Added
[["examples/circuits/post_selection.cpp"](https://github.com/softwareQinc/qpp/blob/main/examples/circuits/post_selection.cpp)]
example
- New feature: implemented support for conditional statements in
["qpp/classes/qcircuit.hpp"]
- Implemented `qpp::QCircuit::`
- `QCircuit& cond_if()` - conditional IF statement
- `QCircuit& cond_else()` - conditional ELSE statement
- `QCircuit& cond_while()` - conditional WHILE statement
- `QCircuit& cond_end()` - conditional END block delimiter
- Added
[["examples/circuits/conditional_if.cpp"](https://github.com/softwareQinc/qpp/blob/main/examples/circuits/conditional_if.cpp)]
and
[["examples/circuits/conditional_while.cpp"](https://github.com/softwareQinc/qpp/blob/main/examples/circuits/conditional_while.cpp)]
examples
- Refactored `qpp::QCircuit::GateStep/MeasurementStep/NOPStep` into separate
files ["qpp/internal/classes/qcircuit_gate_step.hpp"],
["qpp/internal/classes/qcircuit_measurement_step.hpp"], and
Expand Down Expand Up @@ -49,9 +61,9 @@
- Simplified MATLAB detection via CMake `find_package()` function. Users should
only use `-DQPP_MATLAB=ON` when building with MATLAB support, all other
MATLAB-related CMake flags have been removed.
- Bugfix in qpp::adjoint(QCircuit);
- Introduced conditional statements
- cond_func_t type alias
- Bugfix in `qpp::adjoint(QCircuit)`
- Added `cond_func_t` type alias in ["qpp/types.hpp"] for boolean predicates of
the form `std::vector<idx> -> bool`

# Version 5.1 - 1 March 2024

Expand Down
59 changes: 59 additions & 0 deletions examples/circuits/conditional_if.cpp
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';
}
52 changes: 52 additions & 0 deletions examples/circuits/conditional_while.cpp
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';
}
2 changes: 1 addition & 1 deletion examples/circuits/noisy_teleport_qubit_circuit.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Qubit noisy teleportation circuit simulator
// Qubit noisy teleportation quantum circuit simulator
// Source: ./examples/circuits/noisy_teleport_qubit_circuit.cpp

#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion examples/circuits/post_selection.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Qubit post-selection circuit simulator
// Qubit post-selection quantum circuit simulator
// Source: ./examples/circuits/post_selection.cpp

#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion examples/circuits/qpe_circuit.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Quantum phase estimation circuit simulator
// Quantum phase estimation quantum circuit simulator
// Source: ./examples/circuits/qpe_circuit.cpp
// See also ./examples/qpe.cpp for a low-level API example

Expand Down
2 changes: 1 addition & 1 deletion examples/circuits/teleport_qubit_circuit.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Qubit teleportation circuit simulator
// Qubit teleportation quantum circuit simulator
// Source: ./examples/circuits/teleport_qubit_circuit.cpp

#include <iostream>
Expand Down
2 changes: 1 addition & 1 deletion examples/circuits/teleport_qudit_circuit.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Qudit teleportation circuit simulator
// Qudit teleportation quantum circuit simulator
// Source: ./examples/circuits/teleport_qudit_circuit.cpp

#include <iostream>
Expand Down
1 change: 0 additions & 1 deletion include/qpp/types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ struct dirac_t {
* \brief Conditional functor type in qpp::QCircuit conditional statements
*/
using cond_func_t = std::function<bool(std::vector<idx>)>;
// using cond_func_t = bool (*)(std::vector<idx>);

/**
* \brief Quantumly-accessible Random Access Memory (qRAM)
Expand Down

0 comments on commit f881960

Please sign in to comment.