-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperdense.rs
54 lines (42 loc) · 1.29 KB
/
superdense.rs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use qip::prelude::*;
use std::result::Result;
fn main() -> Result<(), CircuitError> {
let mut l = LocalBuilder::<f64>::default();
let e1 = l.qubit();
let e2 = l.qubit();
let a = l.qubit();
let b = l.qubit();
// Hadamard gate on a
let a = l.h(a);
// CNOT gate on a and b
let (a, b) = l.cnot(a, b)?;
// CZ gate on e_1 and a
let mut ce1 = l.condition_with(e1);
let a = ce1.z(a);
let e1 = ce1.dissolve();
// CNOT gate on e_2 and a
let (e2, a) = l.cnot(e2, a)?;
// CNOT gate on a and b
let (a, b) = l.cnot(a, b)?;
// Hadamard gate on a
let a = l.h(a);
// Measure a and b
let (_, handle_a) = l.measure(a);
let (_, handle_b) = l.measure(b);
// Calculate final state
let alice_states = [(0, 0), (0, 1), (1, 0), (1, 1)];
for (alice1, alice2) in alice_states.into_iter() {
let (_, measured) = l.calculate_state_with_init([(&e1, alice1), (&e2, alice2)]);
let (result_a, p_a) = measured.get_measurement(handle_a);
let (result_b, p_b) = measured.get_measurement(handle_b);
println!(
"Input: {}{}, Measured: {}{} with probability: {:.4}",
alice1,
alice2,
result_a,
result_b,
p_a * p_b
);
}
Ok(())
}