Struct quest_bind::QuestEnv
source · pub struct QuestEnv(_);
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
impl QuestEnv
sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
examples/grovers_search.rs (line 69)
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
fn main() -> Result<(), QuestError> {
let env = &QuestEnv::new();
let num_reps = (PI / 4.0 * (NUM_ELEMS as Qreal).sqrt()).ceil() as usize;
println!(
"num_qubits: {NUM_QUBITS}, num_elems: {NUM_ELEMS}, num_reps: \
{num_reps}"
);
// randomly choose the element for which to search
let mut rng = rand::thread_rng();
let sol_elem = rng.gen_range(0..NUM_ELEMS);
// FIXME
const N: i32 = NUM_QUBITS;
// prepare |+>
let mut qureg = Qureg::<'_>::try_new(N, env)?;
qureg.init_plus_state();
// use all qubits in the register
let qubits = &(0..NUM_QUBITS).collect::<Vec<_>>();
// apply Grover's algorithm
(0..num_reps).try_for_each(|_| {
apply_oracle(&mut qureg, qubits, sol_elem)
.and(apply_diffuser(&mut qureg, qubits))
.and(qureg.get_prob_amp(sol_elem))
.map(|prob| {
println!("prob of solution |{sol_elem}> = {:.8}", prob);
})
})
}
More examples
examples/entanglement.rs (line 16)
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
fn main() -> Result<(), QuestError> {
// Initialize QuEST environment and report to screen
let env = &QuestEnv::new();
env.report_quest_env();
// Create a 2-qubit register and report its parameters
let mut qureg = Qureg::try_new(2, env).expect("cannot allocate new Qureg");
qureg.report_qureg_params();
// Initialize |00> state and print out the state to screen
qureg.init_zero_state();
qureg.report_state_to_screen(0);
// Prepare a Bell state `|00> + |11>`: apply Hadamard gate
// on qubit 0, then NOT on qubit 1, controlled by qubit 0.
println!("---\nPrepare Bell state: |00> + |11>");
qureg.hadamard(0).and(qureg.controlled_not(0, 1))?;
// Measure both qubits
let outcome0 = qureg.measure(0)?;
let outcome1 = qureg.measure(1)?;
println!("Qubit \"0\" measured in state: |{outcome0}>");
println!("Qubit \"1\" measured in state: |{outcome1}>");
// Because the state was entangled, the outcomes
// should always be the same
if outcome0 == outcome1 {
println!("They match!");
Ok(())
} else {
panic!("qubits in Bell state should be perfectly correlated");
}
// At this point both `qureg` and `env` are dropped and
// the allocated memory is freed.
}
sourcepub fn sync(&self)
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();
sourcepub fn report_quest_env(&self)
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)
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
fn main() -> Result<(), QuestError> {
// Initialize QuEST environment and report to screen
let env = &QuestEnv::new();
env.report_quest_env();
// Create a 2-qubit register and report its parameters
let mut qureg = Qureg::try_new(2, env).expect("cannot allocate new Qureg");
qureg.report_qureg_params();
// Initialize |00> state and print out the state to screen
qureg.init_zero_state();
qureg.report_state_to_screen(0);
// Prepare a Bell state `|00> + |11>`: apply Hadamard gate
// on qubit 0, then NOT on qubit 1, controlled by qubit 0.
println!("---\nPrepare Bell state: |00> + |11>");
qureg.hadamard(0).and(qureg.controlled_not(0, 1))?;
// Measure both qubits
let outcome0 = qureg.measure(0)?;
let outcome1 = qureg.measure(1)?;
println!("Qubit \"0\" measured in state: |{outcome0}>");
println!("Qubit \"1\" measured in state: |{outcome1}>");
// Because the state was entangled, the outcomes
// should always be the same
if outcome0 == outcome1 {
println!("They match!");
Ok(())
} else {
panic!("qubits in Bell state should be perfectly correlated");
}
// At this point both `qureg` and `env` are dropped and
// the allocated memory is freed.
}
sourcepub fn get_environment_string(&self) -> Result<String, QuestError>
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§
Auto Trait Implementations§
impl RefUnwindSafe for QuestEnv
impl Send for QuestEnv
impl Sync for QuestEnv
impl Unpin for QuestEnv
impl UnwindSafe for QuestEnv
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more