Struct QuestEnv

Source
pub struct QuestEnv(/* private fields */);
Expand description

Information about the QuEST environment.

In practice, this holds info about MPI ranks and helps to hide MPI initialization code.

Implementations§

Source§

impl QuestEnv

Source

pub fn new() -> Self

Create a new environment.

§Examples
let env = QuestEnv::new();
env.report_quest_env();
Examples found in repository?
examples/grovers_search.rs (line 69)
68fn main() -> Result<(), QuestError> {
69    let env = &QuestEnv::new();
70
71    let num_reps = (PI / 4.0 * (NUM_ELEMS as Qreal).sqrt()).ceil() as usize;
72    println!(
73        "num_qubits: {NUM_QUBITS}, num_elems: {NUM_ELEMS}, num_reps: \
74         {num_reps}"
75    );
76    // randomly choose the element for which to search
77    let mut rng = rand::thread_rng();
78    let sol_elem = rng.gen_range(0..NUM_ELEMS);
79
80    // FIXME
81    const N: i32 = NUM_QUBITS;
82    // prepare |+>
83    let mut qureg = Qureg::<'_>::try_new(N, env)?;
84    qureg.init_plus_state();
85    // use all qubits in the register
86    let qubits = &(0..NUM_QUBITS).collect::<Vec<_>>();
87
88    // apply Grover's algorithm
89    (0..num_reps).try_for_each(|_| {
90        apply_oracle(&mut qureg, qubits, sol_elem)
91            .and(apply_diffuser(&mut qureg, qubits))
92            .and(qureg.get_prob_amp(sol_elem))
93            .map(|prob| {
94                println!("prob of solution |{sol_elem}> = {:.8}", prob);
95            })
96    })
97}
More examples
Hide additional examples
examples/entanglement.rs (line 16)
14fn main() -> Result<(), QuestError> {
15    // Initialize QuEST environment and report to screen
16    let env = &QuestEnv::new();
17    env.report_quest_env();
18
19    // Create a 2-qubit register and report its parameters
20    let mut qureg = Qureg::try_new(2, env).expect("cannot allocate new Qureg");
21    qureg.report_qureg_params();
22    // Initialize |00> state and print out the state to screen
23    qureg.init_zero_state();
24    qureg.report_state_to_screen(0);
25
26    // Prepare a Bell state `|00> + |11>`: apply Hadamard gate
27    // on qubit 0, then NOT on qubit 1, controlled by qubit 0.
28    println!("---\nPrepare Bell state: |00> + |11>");
29    qureg.hadamard(0).and(qureg.controlled_not(0, 1))?;
30
31    // Measure both qubits
32    let outcome0 = qureg.measure(0)?;
33    let outcome1 = qureg.measure(1)?;
34    println!("Qubit \"0\" measured in state: |{outcome0}>");
35    println!("Qubit \"1\" measured in state: |{outcome1}>");
36
37    // Because the state was entangled, the outcomes
38    // should always be the same
39    if outcome0 == outcome1 {
40        println!("They match!");
41        Ok(())
42    } else {
43        panic!("qubits in Bell state should be perfectly correlated");
44    }
45
46    // At this point both `qureg` and `env` are dropped and
47    // the allocated memory is freed.
48}
Source

pub fn sync(&self)

Sync environment in distributed mode.

Guarantees that all code up to the given point has been executed on all nodes (if running in distributed mode).

§Examples
let env = QuestEnv::new();
env.sync();
Source

pub fn report_quest_env(&self)

Report information about the QuEST environment.

The information if printed to standard output.

See QuEST API for more information.

Examples found in repository?
examples/entanglement.rs (line 17)
14fn main() -> Result<(), QuestError> {
15    // Initialize QuEST environment and report to screen
16    let env = &QuestEnv::new();
17    env.report_quest_env();
18
19    // Create a 2-qubit register and report its parameters
20    let mut qureg = Qureg::try_new(2, env).expect("cannot allocate new Qureg");
21    qureg.report_qureg_params();
22    // Initialize |00> state and print out the state to screen
23    qureg.init_zero_state();
24    qureg.report_state_to_screen(0);
25
26    // Prepare a Bell state `|00> + |11>`: apply Hadamard gate
27    // on qubit 0, then NOT on qubit 1, controlled by qubit 0.
28    println!("---\nPrepare Bell state: |00> + |11>");
29    qureg.hadamard(0).and(qureg.controlled_not(0, 1))?;
30
31    // Measure both qubits
32    let outcome0 = qureg.measure(0)?;
33    let outcome1 = qureg.measure(1)?;
34    println!("Qubit \"0\" measured in state: |{outcome0}>");
35    println!("Qubit \"1\" measured in state: |{outcome1}>");
36
37    // Because the state was entangled, the outcomes
38    // should always be the same
39    if outcome0 == outcome1 {
40        println!("They match!");
41        Ok(())
42    } else {
43        panic!("qubits in Bell state should be perfectly correlated");
44    }
45
46    // At this point both `qureg` and `env` are dropped and
47    // the allocated memory is freed.
48}
Source

pub fn get_environment_string(&self) -> Result<String, QuestError>

Get a string containing information about the runtime environment,

§Examples
let env = &QuestEnv::new();
let env_str = env.get_environment_string().unwrap();

assert!(env_str.contains("OpenMP="));
assert!(env_str.contains("threads="));
assert!(env_str.contains("MPI="));
assert!(env_str.contains("ranks="));
assert!(env_str.contains("CUDA="));

See QuEST API for more information.

Trait Implementations§

Source§

impl Debug for QuestEnv

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for QuestEnv

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for QuestEnv

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.