prism-q 0.35.0

Fast Rust quantum circuit simulator. OpenQASM 3.0, multiple backends, AVX2 SIMD kernels, optional CUDA and MPI, QEC tooling, Python bindings.
Documentation
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
55
56
57
58
59
60
61
62
63
64
65
66
67
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
//! Structured construction of a [`DynamicProgram`].

use super::{
    Action, BasicBlock, BlockId, ClassicalExpr, ClassicalType, ClassicalValue, DynamicProgram,
    RotationKind, Terminator, VarId, Variable,
};
use crate::circuit::{Circuit, Instruction, SmallVec};
use crate::error::{PrismError, Result};
use crate::gates::Gate;

struct PendingBlock {
    instructions: Vec<Instruction>,
    actions: Vec<Action>,
    terminator: Option<Terminator>,
    loop_id: Option<usize>,
}

enum Frame {
    While {
        loop_id: usize,
        header: usize,
        exit: usize,
    },
    If {
        branch: usize,
        join: usize,
        in_else: bool,
    },
}

/// Builds a [`DynamicProgram`] from structured control flow.
///
/// Instructions append to the block being built. [`begin_while`] and
/// [`begin_if`] open a construct that [`end`] closes; [`begin_else`] switches
/// the innermost `if` to its other arm, or reopens an `if` that [`end`] has
/// just closed. Every index is checked when [`build`] validates the program,
/// so no method here panics on a bad one.
///
/// # Examples
///
/// Repeat until success: prepare `|+>`, measure, and retry while the outcome
/// is 1.
///
/// ```
/// use prism_q::circuit::dynamic::{ClassicalExpr, DynamicProgramBuilder};
/// use prism_q::{Gate, simulate_program};
///
/// let mut b = DynamicProgramBuilder::new(1, 1);
/// b.add_gate(Gate::H, &[0]).add_measure(0, 0);
/// b.begin_while("retry", ClassicalExpr::Bit(0));
/// b.add_reset(0).add_gate(Gate::H, &[0]).add_measure(0, 0);
/// b.end()?;
/// let program = b.build()?;
///
/// let shots = simulate_program(&program).seed(42).shots(100)?;
/// assert!(shots.shots.iter().all(|shot| !shot[0]));
/// # Ok::<(), prism_q::PrismError>(())
/// ```
///
/// [`begin_while`]: Self::begin_while
/// [`begin_if`]: Self::begin_if
/// [`begin_else`]: Self::begin_else
/// [`end`]: Self::end
/// [`build`]: Self::build
pub struct DynamicProgramBuilder {
    num_qubits: usize,
    num_classical_bits: usize,
    variables: Vec<Variable>,
    blocks: Vec<PendingBlock>,
    current: usize,
    frames: Vec<Frame>,
    loop_names: Vec<String>,
    /// The `if` frame [`Self::end`] just closed, which [`Self::begin_else`] may
    /// reopen until anything else is appended.
    reopenable: Option<Frame>,
}

impl DynamicProgramBuilder {
    pub fn new(num_qubits: usize, num_classical_bits: usize) -> Self {
        Self {
            num_qubits,
            num_classical_bits,
            variables: Vec::new(),
            blocks: vec![PendingBlock {
                instructions: Vec::new(),
                actions: Vec::new(),
                terminator: None,
                loop_id: None,
            }],
            current: 0,
            frames: Vec::new(),
            loop_names: Vec::new(),
            reopenable: None,
        }
    }

    pub fn num_qubits(&self) -> usize {
        self.num_qubits
    }

    pub fn num_classical_bits(&self) -> usize {
        self.num_classical_bits
    }

    pub(crate) fn resize(&mut self, num_qubits: usize, num_classical_bits: usize) {
        self.num_qubits = num_qubits;
        self.num_classical_bits = num_classical_bits;
    }

    /// Declare a variable every shot starts at `initial`.
    pub fn declare(
        &mut self,
        name: impl Into<String>,
        ty: ClassicalType,
        initial: ClassicalValue,
    ) -> VarId {
        self.variables.push(Variable {
            name: name.into(),
            ty,
            initial,
        });
        VarId::new(self.variables.len() - 1)
    }

    /// The variable declared under `name`, the latest one when several share it.
    pub fn variable(&self, name: &str) -> Option<VarId> {
        self.variables
            .iter()
            .rposition(|variable| variable.name == name)
            .map(VarId::new)
    }

    /// Parse an OpenQASM expression over this builder's variables, by name, and
    /// its classical bits, read as `c[i]` for one bit and `c` for all of them as
    /// an unsigned integer.
    ///
    /// # Errors
    ///
    /// [`PrismError::Parse`] for text that is not an expression, an unknown
    /// name, or a builtin function over a runtime value.
    pub fn expr(&self, text: &str) -> Result<ClassicalExpr> {
        crate::circuit::openqasm::builder_expr(text, self)
    }

    pub fn add_instruction(&mut self, instruction: Instruction) -> &mut Self {
        let at = self.circuit_block();
        self.blocks[at].instructions.push(instruction);
        self
    }

    pub fn add_gate(&mut self, gate: Gate, targets: &[usize]) -> &mut Self {
        self.add_instruction(Instruction::Gate {
            gate,
            targets: SmallVec::from_slice(targets),
        })
    }

    pub fn add_measure(&mut self, qubit: usize, classical_bit: usize) -> &mut Self {
        self.add_instruction(Instruction::Measure {
            qubit,
            classical_bit,
        })
    }

    pub fn add_reset(&mut self, qubit: usize) -> &mut Self {
        self.add_instruction(Instruction::Reset { qubit })
    }

    /// Append every instruction of `circuit`.
    pub fn append(&mut self, circuit: &Circuit) -> &mut Self {
        let at = self.circuit_block();
        self.blocks[at]
            .instructions
            .extend(circuit.instructions.iter().cloned());
        self
    }

    /// Apply `kind` to `targets` at the angle `angle` evaluates to when the
    /// block runs.
    pub fn add_rotation(
        &mut self,
        kind: RotationKind,
        targets: &[usize],
        angle: ClassicalExpr,
    ) -> &mut Self {
        self.push_action(Action::Rotation {
            kind,
            targets: SmallVec::from_slice(targets),
            angle,
        })
    }

    /// Store `value` into `var`, converted to its declared type.
    pub fn assign(&mut self, var: VarId, value: ClassicalExpr) -> &mut Self {
        self.push_action(Action::Assign { var, value })
    }

    /// Open a loop that runs its body while `condition` is truthy, testing it
    /// before each pass. `name` labels the loop in the step-limit error.
    pub fn begin_while(&mut self, name: impl Into<String>, condition: ClassicalExpr) {
        self.reopenable = None;
        let enclosing = self.blocks[self.current].loop_id;
        let loop_id = self.loop_names.len();
        self.loop_names.push(name.into());
        let header = self.new_block(Some(loop_id));
        let body = self.new_block(Some(loop_id));
        let exit = self.new_block(enclosing);
        self.terminate(Terminator::Jump(BlockId::new(header)));
        self.blocks[header].terminator = Some(Terminator::Branch {
            condition,
            then: BlockId::new(body),
            otherwise: BlockId::new(exit),
        });
        self.frames.push(Frame::While {
            loop_id,
            header,
            exit,
        });
        self.current = body;
    }

    /// Open a branch whose body runs when `condition` is truthy.
    pub fn begin_if(&mut self, condition: ClassicalExpr) {
        self.reopenable = None;
        let loop_id = self.blocks[self.current].loop_id;
        let then = self.new_block(loop_id);
        let join = self.new_block(loop_id);
        self.terminate(Terminator::Branch {
            condition,
            then: BlockId::new(then),
            otherwise: BlockId::new(join),
        });
        self.frames.push(Frame::If {
            branch: self.current,
            join,
            in_else: false,
        });
        self.current = then;
    }

    /// Switch the innermost open `if` to its `else` arm, or reopen the `if`
    /// that [`Self::end`] closed last when nothing has been appended since.
    ///
    /// # Errors
    ///
    /// [`PrismError::InvalidParameter`] when there is no such `if`, or it
    /// already has an `else`.
    pub fn begin_else(&mut self) -> Result<()> {
        let open = matches!(self.frames.last(), Some(Frame::If { in_else: false, .. }));
        let (branch, join) = if open {
            let Some(Frame::If { branch, join, .. }) = self.frames.pop() else {
                unreachable!("checked above")
            };
            self.terminate(Terminator::Jump(BlockId::new(join)));
            (branch, join)
        } else {
            match self.reopenable.take() {
                Some(Frame::If {
                    branch,
                    join,
                    in_else: false,
                }) => (branch, join),
                _ => return Err(misuse("`begin_else` with no `if` to attach to")),
            }
        };
        let other = self.new_block(self.blocks[join].loop_id);
        if let Some(Terminator::Branch { otherwise, .. }) = &mut self.blocks[branch].terminator {
            *otherwise = BlockId::new(other);
        }
        self.frames.push(Frame::If {
            branch,
            join,
            in_else: true,
        });
        self.current = other;
        Ok(())
    }

    /// Close the innermost `while` or `if`.
    ///
    /// # Errors
    ///
    /// [`PrismError::InvalidParameter`] when nothing is open.
    pub fn end(&mut self) -> Result<()> {
        let frame = self
            .frames
            .pop()
            .ok_or_else(|| misuse("`end` with no open `while` or `if`"))?;
        match frame {
            Frame::While { header, exit, .. } => {
                self.terminate(Terminator::Jump(BlockId::new(header)));
                self.current = exit;
                self.reopenable = None;
            }
            Frame::If {
                branch,
                join,
                in_else,
            } => {
                self.terminate(Terminator::Jump(BlockId::new(join)));
                self.current = join;
                self.reopenable = Some(Frame::If {
                    branch,
                    join,
                    in_else,
                });
            }
        }
        Ok(())
    }

    /// Leave the innermost `while`.
    ///
    /// # Errors
    ///
    /// [`PrismError::InvalidParameter`] outside a loop.
    pub fn break_loop(&mut self) -> Result<()> {
        let exit = self
            .innermost_loop()
            .map(|(_, exit)| exit)
            .ok_or_else(|| misuse("`break` outside a `while` loop"))?;
        self.jump_away(exit);
        Ok(())
    }

    /// Skip to the next test of the innermost `while` condition.
    ///
    /// # Errors
    ///
    /// [`PrismError::InvalidParameter`] outside a loop.
    pub fn continue_loop(&mut self) -> Result<()> {
        let header = self
            .innermost_loop()
            .map(|(header, _)| header)
            .ok_or_else(|| misuse("`continue` outside a `while` loop"))?;
        self.jump_away(header);
        Ok(())
    }

    /// Finish the program, dropping unreachable blocks and merging each
    /// straight-line run of blocks into one, so fusion sees the longest
    /// circuits the control flow allows.
    ///
    /// # Errors
    ///
    /// [`PrismError::InvalidParameter`] while a `while` or `if` is still open,
    /// and anything [`DynamicProgram::new`] rejects.
    pub fn build(mut self) -> Result<DynamicProgram> {
        if let Some(frame) = self.frames.last() {
            let what = match frame {
                Frame::While { loop_id, .. } => {
                    format!("`while` loop `{}`", self.loop_names[*loop_id])
                }
                Frame::If { .. } => "`if`".to_string(),
            };
            return Err(misuse(format!("{what} is still open at `build`")));
        }
        for block in &mut self.blocks {
            block.terminator.get_or_insert(Terminator::End);
        }
        let blocks = compact(self.blocks);
        let blocks = blocks
            .into_iter()
            .map(|block| BasicBlock {
                circuit: Circuit {
                    num_qubits: self.num_qubits,
                    num_classical_bits: self.num_classical_bits,
                    instructions: block.instructions,
                },
                actions: block.actions,
                terminator: block.terminator.expect("every block terminated above"),
                loop_name: block.loop_id.map(|id| self.loop_names[id].clone()),
            })
            .collect();
        DynamicProgram::new(
            self.num_qubits,
            self.num_classical_bits,
            self.variables,
            blocks,
        )
    }

    fn new_block(&mut self, loop_id: Option<usize>) -> usize {
        self.blocks.push(PendingBlock {
            instructions: Vec::new(),
            actions: Vec::new(),
            terminator: None,
            loop_id,
        });
        self.blocks.len() - 1
    }

    fn terminate(&mut self, terminator: Terminator) {
        self.blocks[self.current]
            .terminator
            .get_or_insert(terminator);
    }

    /// The block instructions append to: the current one, unless it already
    /// holds an action, which a circuit may not follow within one block.
    fn circuit_block(&mut self) -> usize {
        self.reopenable = None;
        if !self.blocks[self.current].actions.is_empty() {
            let next = self.new_block(self.blocks[self.current].loop_id);
            self.terminate(Terminator::Jump(BlockId::new(next)));
            self.current = next;
        }
        self.current
    }

    fn push_action(&mut self, action: Action) -> &mut Self {
        self.reopenable = None;
        self.blocks[self.current].actions.push(action);
        self
    }

    fn innermost_loop(&self) -> Option<(usize, usize)> {
        self.frames.iter().rev().find_map(|frame| match frame {
            Frame::While { header, exit, .. } => Some((*header, *exit)),
            Frame::If { .. } => None,
        })
    }

    /// End the current block with a jump and continue in a fresh one, which
    /// nothing reaches until a later construct joins into it.
    fn jump_away(&mut self, target: usize) {
        self.reopenable = None;
        self.terminate(Terminator::Jump(BlockId::new(target)));
        self.current = self.new_block(self.blocks[self.current].loop_id);
    }
}

fn misuse(message: impl Into<String>) -> PrismError {
    PrismError::InvalidParameter {
        message: message.into(),
    }
}

/// Thread jumps through empty blocks, merge every block into the one block
/// that jumps to it alone, then drop what the entry cannot reach and renumber
/// in reachable order.
fn compact(mut blocks: Vec<PendingBlock>) -> Vec<PendingBlock> {
    let forward = |blocks: &[PendingBlock], start: usize| {
        let mut at = start;
        for _ in 0..blocks.len() {
            let block = &blocks[at];
            match block.terminator {
                Some(Terminator::Jump(next))
                    if block.instructions.is_empty()
                        && block.actions.is_empty()
                        && next.index() != at =>
                {
                    at = next.index();
                }
                _ => break,
            }
        }
        at
    };
    let threaded: Vec<usize> = (0..blocks.len()).map(|at| forward(&blocks, at)).collect();
    for block in &mut blocks {
        retarget(block, |id| BlockId::new(threaded[id.index()]));
    }
    let entry = threaded[0];

    let mut predecessors = vec![0usize; blocks.len()];
    for block in &blocks {
        for target in successors(block) {
            predecessors[target] += 1;
        }
    }
    for at in 0..blocks.len() {
        while let Some(Terminator::Jump(target)) = blocks[at].terminator {
            let from = target.index();
            if !blocks[at].actions.is_empty()
                || from == at
                || from == entry
                || predecessors[from] != 1
            {
                break;
            }
            let absorbed = std::mem::replace(
                &mut blocks[from],
                PendingBlock {
                    instructions: Vec::new(),
                    actions: Vec::new(),
                    terminator: Some(Terminator::End),
                    loop_id: None,
                },
            );
            let block = &mut blocks[at];
            block.instructions.extend(absorbed.instructions);
            block.actions = absorbed.actions;
            block.terminator = absorbed.terminator;
        }
    }

    let mut order = Vec::with_capacity(blocks.len());
    let mut renumber = vec![usize::MAX; blocks.len()];
    let mut stack = vec![entry];
    while let Some(at) = stack.pop() {
        if renumber[at] != usize::MAX {
            continue;
        }
        renumber[at] = order.len();
        order.push(at);
        for target in successors(&blocks[at]).into_iter().rev() {
            if renumber[target] == usize::MAX {
                stack.push(target);
            }
        }
    }
    let mut slots: Vec<Option<PendingBlock>> = blocks.into_iter().map(Some).collect();
    order
        .iter()
        .map(|&at| {
            let mut block = slots[at].take().expect("each block visited once");
            retarget(&mut block, |id| BlockId::new(renumber[id.index()]));
            block
        })
        .collect()
}

fn successors(block: &PendingBlock) -> SmallVec<[usize; 2]> {
    match block.terminator.as_ref().expect("every block terminated") {
        Terminator::Jump(target) => SmallVec::from_slice(&[target.index()]),
        Terminator::Branch {
            then, otherwise, ..
        } => SmallVec::from_slice(&[then.index(), otherwise.index()]),
        Terminator::End => SmallVec::new(),
    }
}

fn retarget(block: &mut PendingBlock, map: impl Fn(BlockId) -> BlockId) {
    match block.terminator.as_mut().expect("every block terminated") {
        Terminator::Jump(target) => *target = map(*target),
        Terminator::Branch {
            then, otherwise, ..
        } => {
            *then = map(*then);
            *otherwise = map(*otherwise);
        }
        Terminator::End => {}
    }
}