-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathmeasurements1.cpp
33 lines (26 loc) · 1016 Bytes
/
measurements1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Measurements
// Source: ./examples/measurements1.cpp
#include <iostream>
#include <tuple>
#include "qpp/qpp.hpp"
int main() {
using namespace qpp;
ket psi = 00_ket;
cmat U = gt.CNOT * kron(gt.H, gt.Id2);
ket result = U * psi; // we have the Bell state (|00> + |11>) / sqrt(2)
std::cout << ">> We just produced the Bell state:\n";
std::cout << disp(result) << '\n';
// apply a bit flip on the second qubit
result = apply(result, gt.X, {1}); // we produced (|01> + |10>) / sqrt(2)
std::cout << ">> We produced the Bell state:\n";
std::cout << disp(result) << '\n';
// measure the first qubit in the X basis
auto [m, probs, states] = measure(result, gt.H, {0});
std::cout << ">> Measurement result: " << m << '\n';
std::cout << ">> Probabilities: ";
std::cout << disp(probs, IOManipContainerOpts{}.set_sep(", ")) << '\n';
std::cout << ">> Resulting states:\n";
for (auto&& elem : states) {
std::cout << disp(elem) << "\n\n";
}
}