Skip to content

Instantly share code, notes, and snippets.

@omgbbqhaxx
Last active April 1, 2023 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omgbbqhaxx/5c31d0c6372f3609ac11144fb723b632 to your computer and use it in GitHub Desktop.
Save omgbbqhaxx/5c31d0c6372f3609ac11144fb723b632 to your computer and use it in GitHub Desktop.
simple quantum hademard gates
extern crate q1tsim;
use q1tsim::{circuit, gates};
fn main() {
// The number of times this circuit is evaluated
let nr_runs = 8192;
// Create a quantum circuit with 3 quantum bits and 3 classical (measurement)
// bits. The circuit starts by default with all quantum bits in the |0⟩ state,
// so in this case |000⟩.
let mut circuit = circuit::Circuit::new(3, 3);
// Apply an X gate to all qubits after measurement
circuit.h(0);
circuit.h(1);
circuit.h(2);
circuit.measure_all(&[0, 1, 2]);
// Actually calculate the resulting quantum state and perform the measurements,
// averaging over `nr_runs` runs.
circuit.execute(nr_runs);
// And print the results.
let hist = circuit.histogram_string().unwrap();
let mut sorted_hist = hist.iter().collect::<Vec<_>>();
sorted_hist.sort_by_key(|(_, count)| *count); // Sort by count, ascending
for (bits, count) in sorted_hist {
println!("{}: {}", bits, count);
}
//println!("{:?}", hist);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment