Crate quantr

source ·
Expand description

Construct, simulate and measure quantum circuits.

Initialise a new quantum circuit by using Circuit::new where the argument defines the number of qubits. Afterwards, various methods can be called to append gates onto the circuit in columns. For instance, Circuit::add_gate will add a single gate, whilst Circuit::add_gates_with_positions and Circuit::add_repeating_gate will add multiple.

Before committing to a simulation, the circuit can be printed to the console or exported as a UTF-8 string to an external file using Printer::print_diagram and Printer::save_diagram respectively. The printer is created with Printer::new an by passing a reference to the circuit that should be printed.

The circuit can then be simulated with Circuit::simulate. The progress of the simulation can be printed to the terminal by calling Circuit::toggle_simulation_progress before simulating the circuit.

A bin count of states that are observed over a period of measurements can be performed with Circuit::repeat_measurement, where a new register is attached before each measurement. Or, the explicit superposition can be retrieved using Circuit::get_superposition.

All errors resulting from the incorrect use of quantr are propagated by QuantrError and QuantrErrorConst that implement the std::error::Error trait.

More complex examples can be found in the examples folder within this repository.

§Example

use quantr::{Circuit, Gate, Printer, Measurement::Observable};

let mut quantum_circuit: Circuit = Circuit::new(2).unwrap();

quantum_circuit
    .add_gates(&[Gate::H, Gate::Y]).unwrap()
    .add_gate(Gate::CNot(0), 1).unwrap();

let mut printer = Printer::new(&quantum_circuit);
printer.print_diagram();
// The above prints the following:
// ┏━━━┓     
// ┨ H ┠──█──
// ┗━━━┛  │  
//        │  
// ┏━━━┓┏━┷━┓
// ┨ Y ┠┨ X ┠
// ┗━━━┛┗━━━┛

quantum_circuit.simulate();

// Below prints the number of times that each state was observered
// over 500 measurements of superpositions.

if let Ok(Observable(bin_count)) = quantum_circuit.repeat_measurement(500) {
    println!("[Observable] Bin count of observed states.");
    for (state, count) in bin_count {
        println!("|{}> observed {} times", state, count);
    }
}

Modules§

  • Defines the qubit, product states and super positions including relevant operations.

Macros§

  • Usage: complex!(re: f64, im: f64) -> Complex<f64> A quick way to define a f64 complex number.
  • Usage: complex_im!(im: f64) -> Complex<f64> A quick way to define an imaginary f64; the real part is set to zero.
  • Usage: complex_im_array!(input: [f64; n]) -> [Complex<f64>; n] Returns an array of complex number with zero real part, and imaginaries set by input.
  • Usage: complex_im_vec!(input: [f64; n]) -> Vec<Complex<f64>> Returns a vector of complex numbers with zero real part, and imaginaries set by input.
  • Usage: complex_re!(re: f64) -> Complex<f64> A quick way to define a real f64; the imaginary part is set to zero.
  • Usage: complex_re_array!(input: [f64; n]) -> [Complex<f64>; n] Returns an array of complex numbers with zero imaginary part, and the real part set by input.
  • Usage: complex_re_vec!(input: [f64; n]) -> Vec<Complex<f64>> Returns a vector of complex number with zero imaginary part, and reals set by input.

Structs§

  • A quantum circuit where gates can be appended and then simulated to measure resulting superpositions.
  • Generic complex number.
  • Constructs, displays and saves the circuit diagram as a UTF-8 string.

Enums§

Constants§