ticit 0.2.2

Fast simulation of near-Clifford quantum circuits.
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
//! Symbol values, records and detectors: one bit-plane of shots per
//! condition/record/detector, XOR-combined a word (64 shots) at a time.

use super::{
    BATCH_SCALAR_SYMBOLIC_EVAL_THRESHOLD, BatchFactoredExecutorState, batch_condition_offset,
    batch_detector_offset, batch_live_word_mask, batch_record_offset, fill_batch_bits,
    invert_batch_bits, mask_batch_bits, runtime_batch_word_count,
};
use crate::bits::{symbol_bit_mask, symbol_word_index};
use crate::errors::{Result, TicitError};
#[cfg(test)]
use crate::factored::FactoredInstructionProgram;
use crate::symbolic::SymbolicBoolEvaluationPlan;

pub(crate) fn check_batch_symbol_slot(
    runtime: &BatchFactoredExecutorState,
    condition: i32,
) -> Result<()> {
    if condition <= 0 || condition as usize > runtime.nsymbols {
        return Err(TicitError::new(
            "symbolic condition exceeds batch executor symbol table",
        ));
    }
    Ok(())
}

pub(crate) fn batch_symbol_assigned(
    runtime: &BatchFactoredExecutorState,
    condition: i32,
) -> Result<bool> {
    check_batch_symbol_slot(runtime, condition)?;
    let word = symbol_word_index(condition);
    Ok(word < runtime.assigned_words.len()
        && (runtime.assigned_words[word] & symbol_bit_mask(condition)) != 0)
}

fn mark_batch_symbol_assigned_unchecked(runtime: &mut BatchFactoredExecutorState, condition: i32) {
    runtime.assigned_words[symbol_word_index(condition)] |= symbol_bit_mask(condition);
}

fn batch_symbol_matches_bits(
    runtime: &BatchFactoredExecutorState,
    condition: i32,
    bits: &[u64],
) -> Result<bool> {
    let nwords = runtime_batch_word_count(runtime);
    if bits.len() < nwords {
        return Err(TicitError::new("batch bit vector is too short"));
    }
    for word in 0..nwords {
        let mask = batch_live_word_mask(runtime, word);
        let actual = runtime.value_words[batch_condition_offset(runtime, condition, word)];
        if (actual ^ bits[word]) & mask != 0 {
            return Ok(false);
        }
    }
    Ok(true)
}

fn copy_bits_to_batch_symbol_unchecked(
    runtime: &mut BatchFactoredExecutorState,
    condition: i32,
    bits: &[u64],
) -> Result<()> {
    let nwords = runtime_batch_word_count(runtime);
    if bits.len() < nwords {
        return Err(TicitError::new("batch bit vector is too short"));
    }
    let base = batch_condition_offset(runtime, condition, 0);
    if nwords == runtime.batch_words && runtime.active_shots & 63 == 0 {
        runtime.value_words[base..base + nwords].copy_from_slice(&bits[..nwords]);
        return Ok(());
    }
    for word in 0..nwords {
        runtime.value_words[base + word] = bits[word] & batch_live_word_mask(runtime, word);
    }
    for word in nwords..runtime.batch_words {
        runtime.value_words[base + word] = 0;
    }
    Ok(())
}

/// Idempotent-with-check assignment: re-assigning must match the stored bits.
pub(crate) fn assign_batch_symbol(
    runtime: &mut BatchFactoredExecutorState,
    condition: i32,
    bits: &[u64],
) -> Result<()> {
    check_batch_symbol_slot(runtime, condition)?;
    if batch_symbol_assigned(runtime, condition)? {
        if !batch_symbol_matches_bits(runtime, condition, bits)? {
            return Err(TicitError::new(
                "symbolic condition was assigned inconsistent concrete batch values",
            ));
        }
        return Ok(());
    }
    mark_batch_symbol_assigned_unchecked(runtime, condition);
    copy_bits_to_batch_symbol_unchecked(runtime, condition, bits)
}

pub(crate) fn assign_batch_symbol_opt(
    runtime: &mut BatchFactoredExecutorState,
    condition: Option<i32>,
    bits: &[u64],
) -> Result<()> {
    match condition {
        Some(condition) => assign_batch_symbol(runtime, condition, bits),
        None => Ok(()),
    }
}

// ==============================================================================
// Batched symbolic evaluation
// ==============================================================================

fn check_condition_assigned(runtime: &BatchFactoredExecutorState, condition: i32) -> Result<()> {
    if !batch_symbol_assigned(runtime, condition)? {
        return Err(TicitError::new(
            "symbolic condition expression has no concrete batch value",
        ));
    }
    Ok(())
}

fn check_batch_symbolic_plan_assigned(
    plan: &SymbolicBoolEvaluationPlan,
    runtime: &BatchFactoredExecutorState,
) -> Result<()> {
    if plan.word_indices.is_empty() {
        for &condition in &plan.conditions {
            check_condition_assigned(runtime, condition)?;
        }
        return Ok(());
    }
    let max_word = *plan.word_indices.last().expect("nonempty checked above");
    if max_word >= runtime.assigned_words.len() {
        return Err(TicitError::new(
            "symbolic condition expression has no concrete batch value",
        ));
    }
    let mut missing = 0u64;
    for (i, &word) in plan.word_indices.iter().enumerate() {
        missing |= plan.word_masks[i] & !runtime.assigned_words[word];
    }
    if missing != 0 {
        return Err(TicitError::new(
            "symbolic condition expression has no concrete batch value",
        ));
    }
    Ok(())
}

/// Evaluates `plan` for the whole batch: fills `out` with the constant, then
/// XORs one condition column per condition.
///
/// The `nwords ∈ {1, 2}` shapes are unrolled with register accumulators, and
/// small plans check each condition's assignment inline while large plans do
/// one bulk word-mask test first.
pub fn eval_symbolic_bool_batch(
    out: &mut Vec<u64>,
    plan: &SymbolicBoolEvaluationPlan,
    runtime: &BatchFactoredExecutorState,
) -> Result<()> {
    fill_batch_bits(out, runtime, plan.constant);
    if plan.conditions.is_empty() {
        return Ok(());
    }
    let nwords = runtime_batch_word_count(runtime);
    let checked_per_condition = plan.word_indices.is_empty()
        || plan.conditions.len() <= BATCH_SCALAR_SYMBOLIC_EVAL_THRESHOLD;
    if !checked_per_condition {
        check_batch_symbolic_plan_assigned(plan, runtime)?;
    }
    match nwords {
        1 => {
            let mut out0 = out[0];
            if runtime.batch_words == 1 {
                for &condition in &plan.conditions {
                    if checked_per_condition {
                        check_condition_assigned(runtime, condition)?;
                    }
                    out0 ^= runtime.value_words[(condition - 1) as usize];
                }
            } else {
                for &condition in &plan.conditions {
                    if checked_per_condition {
                        check_condition_assigned(runtime, condition)?;
                    }
                    out0 ^= runtime.value_words[batch_condition_offset(runtime, condition, 0)];
                }
            }
            out[0] = out0;
        }
        2 => {
            let mut out0 = out[0];
            let mut out1 = out[1];
            for &condition in &plan.conditions {
                if checked_per_condition {
                    check_condition_assigned(runtime, condition)?;
                }
                let base = batch_condition_offset(runtime, condition, 0);
                out0 ^= runtime.value_words[base];
                out1 ^= runtime.value_words[base + 1];
            }
            out[0] = out0;
            out[1] = out1;
        }
        _ => {
            for &condition in &plan.conditions {
                if checked_per_condition {
                    check_condition_assigned(runtime, condition)?;
                }
                let base = batch_condition_offset(runtime, condition, 0);
                for word in 0..nwords {
                    out[word] ^= runtime.value_words[base + word];
                }
            }
        }
    }
    mask_batch_bits(out, runtime);
    Ok(())
}

/// Like [`eval_symbolic_bool_batch`] but XOR-accumulates into an existing
/// buffer — the residual half of a presampled expression.
pub(crate) fn xor_symbolic_bool_batch_into(
    out: &mut Vec<u64>,
    plan: &SymbolicBoolEvaluationPlan,
    runtime: &BatchFactoredExecutorState,
) -> Result<()> {
    let nwords = runtime_batch_word_count(runtime);
    if out.len() < runtime.batch_words {
        out.resize(runtime.batch_words, 0);
    }
    if plan.conditions.is_empty() {
        if plan.constant {
            invert_batch_bits(out, runtime);
        }
        return Ok(());
    }
    let checked_per_condition = plan.word_indices.is_empty()
        || plan.conditions.len() <= BATCH_SCALAR_SYMBOLIC_EVAL_THRESHOLD;
    if !checked_per_condition {
        check_batch_symbolic_plan_assigned(plan, runtime)?;
    }
    if nwords == 1 {
        let mut out0 = out[0]
            ^ if plan.constant {
                batch_live_word_mask(runtime, 0)
            } else {
                0
            };
        if runtime.batch_words == 1 {
            for &condition in &plan.conditions {
                if checked_per_condition {
                    check_condition_assigned(runtime, condition)?;
                }
                out0 ^= runtime.value_words[(condition - 1) as usize];
            }
        } else {
            for &condition in &plan.conditions {
                if checked_per_condition {
                    check_condition_assigned(runtime, condition)?;
                }
                out0 ^= runtime.value_words[batch_condition_offset(runtime, condition, 0)];
            }
        }
        out[0] = out0 & batch_live_word_mask(runtime, 0);
        out[1..].fill(0);
        return Ok(());
    }
    if nwords == 2 {
        let mut out0 = out[0]
            ^ if plan.constant {
                batch_live_word_mask(runtime, 0)
            } else {
                0
            };
        let mut out1 = out[1]
            ^ if plan.constant {
                batch_live_word_mask(runtime, 1)
            } else {
                0
            };
        for &condition in &plan.conditions {
            if checked_per_condition {
                check_condition_assigned(runtime, condition)?;
            }
            let base = batch_condition_offset(runtime, condition, 0);
            out0 ^= runtime.value_words[base];
            out1 ^= runtime.value_words[base + 1];
        }
        out[0] = out0 & batch_live_word_mask(runtime, 0);
        out[1] = out1 & batch_live_word_mask(runtime, 1);
        out[2..].fill(0);
        return Ok(());
    }
    if plan.constant {
        for word in 0..nwords {
            out[word] ^= batch_live_word_mask(runtime, word);
        }
    }
    for &condition in &plan.conditions {
        if checked_per_condition {
            check_condition_assigned(runtime, condition)?;
        }
        let base = batch_condition_offset(runtime, condition, 0);
        for word in 0..nwords {
            out[word] ^= runtime.value_words[base + word];
        }
    }
    mask_batch_bits(out, runtime);
    out[nwords..].fill(0);
    Ok(())
}

// ==============================================================================
// Records and detectors
// ==============================================================================

/// Growing the record count changes the column stride, so every existing
/// column must be recopied — a plain resize would interleave old columns.
fn ensure_batch_measurement_storage(runtime: &mut BatchFactoredExecutorState, record: i32) {
    if record as usize <= runtime.nrecords {
        return;
    }
    let mut next = vec![0u64; record as usize * runtime.batch_words];
    for old_record in 1..=runtime.nrecords as i32 {
        let old_base = batch_record_offset(runtime, old_record, 0);
        let new_base = (old_record - 1) as usize * runtime.batch_words;
        next[new_base..new_base + runtime.batch_words]
            .copy_from_slice(&runtime.measurement_words[old_base..old_base + runtime.batch_words]);
    }
    runtime.nrecords = record as usize;
    runtime.measurement_words = next;
}

fn ensure_batch_detector_storage(runtime: &mut BatchFactoredExecutorState, detector: i32) {
    if detector as usize <= runtime.ndetectors {
        return;
    }
    let mut next = vec![0u64; detector as usize * runtime.batch_words];
    for old_detector in 1..=runtime.ndetectors as i32 {
        let old_base = batch_detector_offset(runtime, old_detector, 0);
        let new_base = (old_detector - 1) as usize * runtime.batch_words;
        next[new_base..new_base + runtime.batch_words]
            .copy_from_slice(&runtime.detector_words[old_base..old_base + runtime.batch_words]);
    }
    runtime.ndetectors = detector as usize;
    runtime.detector_words = next;
}

pub(crate) fn write_batch_measurement_record(
    runtime: &mut BatchFactoredExecutorState,
    record: Option<i32>,
    outcome_bits: &[u64],
    record_condition: Option<i32>,
) -> Result<()> {
    let nwords = runtime_batch_word_count(runtime);
    if let Some(record) = record {
        if record <= 0 {
            return Err(TicitError::new("measurement record id must be positive"));
        }
        ensure_batch_measurement_storage(runtime, record);
        let base = batch_record_offset(runtime, record, 0);
        if nwords == runtime.batch_words && runtime.active_shots & 63 == 0 {
            runtime.measurement_words[base..base + nwords].copy_from_slice(&outcome_bits[..nwords]);
        } else {
            for word in 0..nwords {
                runtime.measurement_words[base + word] =
                    outcome_bits[word] & batch_live_word_mask(runtime, word);
            }
            for word in nwords..runtime.batch_words {
                runtime.measurement_words[base + word] = 0;
            }
        }
    }
    assign_batch_symbol_opt(runtime, record_condition, outcome_bits)
}

/// Fast path: an outcome plan that is exactly the branch condition (possibly
/// negated) can reuse the branch bits sitting in the caller's buffer instead
/// of re-evaluating the plan. Returns whether it fired.
pub(crate) fn write_direct_branch_measurement_record(
    runtime: &mut BatchFactoredExecutorState,
    branch_bits: &mut [u64],
    branch_condition: i32,
    outcome_plan: &SymbolicBoolEvaluationPlan,
    record: Option<i32>,
    record_condition: Option<i32>,
) -> Result<bool> {
    if outcome_plan.conditions.len() != 1 || outcome_plan.conditions[0] != branch_condition {
        return Ok(false);
    }
    if outcome_plan.constant {
        invert_batch_bits(branch_bits, runtime);
    }
    write_batch_measurement_record(runtime, record, branch_bits, record_condition)?;
    Ok(true)
}

pub(crate) fn write_batch_detector_record(
    runtime: &mut BatchFactoredExecutorState,
    detector: i32,
    outcome_bits: &[u64],
) -> Result<()> {
    let nwords = runtime_batch_word_count(runtime);
    if detector <= 0 {
        return Err(TicitError::new("detector id must be positive"));
    }
    if !runtime.store_detector_records {
        for word in 0..nwords {
            runtime.detector_any_words[word] |=
                outcome_bits[word] & batch_live_word_mask(runtime, word);
        }
        return Ok(());
    }
    ensure_batch_detector_storage(runtime, detector);
    let base = batch_detector_offset(runtime, detector, 0);
    if nwords == runtime.batch_words && runtime.active_shots & 63 == 0 {
        runtime.detector_words[base..base + nwords].copy_from_slice(&outcome_bits[..nwords]);
        for word in 0..nwords {
            runtime.detector_any_words[word] |= outcome_bits[word];
        }
    } else {
        for word in 0..nwords {
            let bits = outcome_bits[word] & batch_live_word_mask(runtime, word);
            runtime.detector_words[base + word] = bits;
            runtime.detector_any_words[word] |= bits;
        }
        for word in nwords..runtime.batch_words {
            runtime.detector_words[base + word] = 0;
        }
    }
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::symbolic::{SymbolicBool, SymbolicBoolEvaluationPlan};

    #[test]
    fn residual_xor_fast_shapes_match_wordwise_parity() {
        let program = FactoredInstructionProgram {
            nsymbols: 2,
            ..Default::default()
        };
        let plan = SymbolicBoolEvaluationPlan::new(&SymbolicBool::new(true, vec![1, 2]));
        for (capacity, shots) in [(64, 37), (128, 37), (128, 128)] {
            let mut runtime = BatchFactoredExecutorState::new(&program, capacity, 1)
                .expect("batch runtime builds");
            runtime.active_shots = shots;
            let first = [0x0f0f_0f0f_0f0f_0f0f, 0xaaaa_aaaa_aaaa_aaaa];
            let second = [0x3333_3333_3333_3333, 0x5555_5555_5555_5555];
            assign_batch_symbol(&mut runtime, 1, &first).expect("first symbol assigns");
            assign_batch_symbol(&mut runtime, 2, &second).expect("second symbol assigns");

            let mut actual = vec![0x0123_4567_89ab_cdef; runtime.batch_words];
            let mut expected = actual.clone();
            let nwords = runtime_batch_word_count(&runtime);
            for word in 0..nwords {
                expected[word] = (expected[word] ^ first[word] ^ second[word])
                    ^ batch_live_word_mask(&runtime, word);
                expected[word] &= batch_live_word_mask(&runtime, word);
            }
            expected[nwords..].fill(0);

            xor_symbolic_bool_batch_into(&mut actual, &plan, &runtime)
                .expect("residual expression evaluates");
            assert_eq!(actual, expected, "capacity={capacity}, shots={shots}");
        }
    }
}