dqcsim 0.0.4

DQCsim: Delft Quantum Classical Simulator
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use super::*;
use std::collections::VecDeque;
use std::ptr::null_mut;
use std::thread::JoinHandle;

pub type ArbCmdQueue = VecDeque<ArbCmd>;

pub type QubitReferenceSet = VecDeque<QubitRef>;

pub type QubitMeasurementResultSet = HashMap<QubitRef, QubitMeasurementResult>;

pub type PluginJoinHandle = JoinHandle<Result<()>>;

pub type BoxedPluginConfiguration = Box<dyn PluginConfiguration>;

macro_rules! api_object_types {
    ($($(#[$m:meta])* $i:ident,)+) => (
        /// Enumeration of all objects that can be associated with an handle, including
        /// the object data.
        #[derive(Debug)]
        #[allow(dead_code, clippy::large_enum_variant)]
        pub enum APIObject {
            $(
                $(#[$m])*
                $i($i),
            )+
        }

        $(
            impl From<$i> for APIObject {
                fn from(x: $i) -> APIObject {
                    APIObject::$i(x)
                }
            }
        )+
    )
}

api_object_types!(
    /// `ArbData` object.
    ArbData,
    /// `ArbCmd` object.
    ArbCmd,
    /// Queue of `ArbCmd` objects.
    ArbCmdQueue,
    /// Set of qubit references.
    QubitReferenceSet,
    /// Quantum gate object.
    Gate,
    /// Qubit measurement object.
    QubitMeasurementResult,
    /// Set of qubit measurement objects.
    QubitMeasurementResultSet,
    /// `PluginProcessConfiguration` object.
    PluginProcessConfiguration,
    /// `PluginThreadConfiguration` object.
    PluginThreadConfiguration,
    /// `SimulatorConfiguration` object.
    SimulatorConfiguration,
    /// DQCsim simulation instance, behaving as an accelerator.
    Simulator,
    /// `PluginDefinition` object.
    PluginDefinition,
    /// Join handle for a plugin thread.
    PluginJoinHandle,
);

/// Thread-local state storage structure.
pub struct APIState {
    /// Mapping from handle to object.
    ///
    /// This contains all API-managed data.
    pub objects: HashMap<dqcs_handle_t, APIObject>,

    /// This variable stores the handle value that will be used for the next
    /// allocation.
    pub handle_counter: dqcs_handle_t,

    /// The handle that currently owns DQCsim's thread-local resources. This
    /// serves as a lock to ensure that no more than one object using
    /// thread-locals is constructed.
    pub thread_locals_used_by: Option<dqcs_handle_t>,

    /// This variable records the error message associated with the latest
    /// failure.
    pub last_error: Option<CString>,
}

impl APIState {
    /// Stuffs the given object into the API-managed storage. Returns the
    /// handle created for it.
    pub fn push(&mut self, object: APIObject) -> dqcs_handle_t {
        let handle = self.handle_counter;
        self.objects.insert(handle, object);
        self.handle_counter = handle + 1;
        handle
    }

    /// Claims the thread-local storage lock for the given handle.
    ///
    /// The lock is released when the handle becomes invalid.
    pub fn thread_locals_claim(&mut self, handle: dqcs_handle_t) -> Result<()> {
        self.thread_locals_assert_free()?;
        self.thread_locals_used_by.replace(handle);
        Ok(())
    }

    /// Returns whether DQCsim's thread-locals are currently in use.
    pub fn thread_locals_claimed(&self) -> bool {
        if let Some(h) = self.thread_locals_used_by {
            self.objects.contains_key(&h)
        } else {
            false
        }
    }

    /// Asserts that DQCsim's thread-locals are not in use.
    pub fn thread_locals_assert_free(&self) -> Result<()> {
        if self.thread_locals_claimed() {
            inv_op(format!(
                "cannot claim DQCsim thread-local storage; already claimed by handle {}",
                self.thread_locals_used_by.unwrap()
            ))
        } else {
            Ok(())
        }
    }
}

impl Drop for APIState {
    fn drop(&mut self) {
        let mut warn = false;
        for (_, v) in self.objects.drain() {
            if let APIObject::Simulator(_) = v {
                warn = true;
                std::mem::forget(v);
            }
        }
        if warn {
            eprintln!(
                "DQCsim API error: you've leaked one or more Simulator objects! \
                 You should always call dqcs_handle_delete() on simulator objects or call \
                 dqcs_handle_delete_all() to delete all handles before exiting, otherwise \
                 things are not destroyed in the right order."
            );
        }
    }
}

thread_local! {
    /// Thread-local state storage. Be careful not to call user callback functions
    /// while holding a reference to the state: those callbacks can and probably
    /// will claim the reference mutably at some point. Basically, once user
    /// callbacks need to become "callable" from the Rust world, for instance when
    /// a configuration object is consumed into the object it configures, they
    /// should move out of this state.
    pub static API_STATE: RefCell<APIState> = RefCell::new(APIState {
        objects: HashMap::new(),
        handle_counter: 1,
        thread_locals_used_by: None,
        last_error: None,
    });
}

/// Convenience function for converting a Result to an API return value and
/// possibly an error string.
///
/// Sets the `last_error` field in the `APIState` and returns `error_value` if
/// the result of `call()` is `Err`. Otherwise, the result of the callback is
/// returned without modification.
pub fn api_return<T>(error_value: T, call: impl FnOnce() -> Result<T>) -> T {
    match call() {
        Ok(x) => x,
        Err(e) => {
            API_STATE.with(|state| {
                state.borrow_mut().last_error.replace(
                    CString::new(e.to_string())
                        .unwrap_or_else(|_| CString::new("<UNKNOWN>").unwrap()),
                )
            });
            error_value
        }
    }
}

/// Same as `api_return()`, but specialized for `dqcs_return_t`.
pub fn api_return_none(call: impl FnOnce() -> Result<()>) -> dqcs_return_t {
    api_return(dqcs_return_t::DQCS_FAILURE, || {
        call().map(|()| dqcs_return_t::DQCS_SUCCESS)
    })
}

/// Same as `api_return()`, but specialized for `dqcs_bool_return_t`.
pub fn api_return_bool(call: impl FnOnce() -> Result<bool>) -> dqcs_bool_return_t {
    api_return(dqcs_bool_return_t::DQCS_BOOL_FAILURE, || {
        call().map(dqcs_bool_return_t::from)
    })
}

/// Same as `api_return()`, but specialized for returning strings.
pub fn api_return_string(call: impl FnOnce() -> Result<String>) -> *mut c_char {
    api_return(null_mut(), || {
        call().and_then(|s| {
            let s = CString::new(&s[..])?;
            let s = unsafe { strdup(s.as_ptr()) as *mut c_char };
            if s.is_null() {
                err("failed to allocate return value")
            } else {
                Ok(s)
            }
        })
    })
}

/// Convenience function for converting an API callback return value to a
/// Result object.
///
/// If `actual_value` equals `error_value`, `Err` is returned, taking the
/// message from the `last_error` field in the `APIState`. Otherwise, `Ok`
/// is returned, containing `actual_value`.
pub fn cb_return<T>(error_value: T, actual_value: T) -> Result<T>
where
    T: std::cmp::PartialEq,
{
    if actual_value != error_value {
        Ok(actual_value)
    } else {
        API_STATE.with(|state| {
            let state = state.borrow();
            if let Some(e) = state.last_error.as_ref() {
                err(e
                    .clone()
                    .into_string()
                    .unwrap_or_else(|_| "Unknown error".to_string()))
            } else {
                err("Unknown error")
            }
        })
    }
}

/// Same as `cb_return()`, but specialized for `dqcs_return_t`.
pub fn cb_return_none(actual_value: dqcs_return_t) -> Result<()> {
    cb_return(dqcs_return_t::DQCS_FAILURE, actual_value).map(|_| ())
}

/// Structure used to access objects stored in the thread-local object pool.
///
/// While this object is in scope, the API object is removed from the pool.
/// Therefore, all forms of aliasing are prevented
pub struct ResolvedHandle {
    ob: Option<APIObject>,
    handle: dqcs_handle_t,
}

impl Drop for ResolvedHandle {
    /// If no ownership was taken over the API object with the given handle,
    /// insert it back into the thread-local object pool.
    fn drop(&mut self) {
        if let Some(ob) = self.ob.take() {
            API_STATE.with(|state| state.borrow_mut().objects.insert(self.handle, ob));
        }
    }
}

pub trait UseHandleAs<T> {
    /// Obtains an immutable reference to the embedded API object using the
    /// interface identified by T.
    fn as_ref(&self) -> Result<&T>;

    /// Obtains a mutable reference to the embedded API object using the
    /// interface identified by T.
    fn as_mut(&mut self) -> Result<&mut T>;

    /// Takes ownership of the embedded API object using the interface
    /// identified by T.
    fn take(&mut self) -> Result<T>;
}

impl UseHandleAs<APIObject> for ResolvedHandle {
    fn as_ref(&self) -> Result<&APIObject> {
        Ok(self
            .ob
            .as_ref()
            .expect("object ownership was already given away"))
    }

    fn as_mut(&mut self) -> Result<&mut APIObject> {
        Ok(self
            .ob
            .as_mut()
            .expect("object ownership was already given away"))
    }

    fn take(&mut self) -> Result<APIObject> {
        Ok(self
            .ob
            .take()
            .expect("object ownership was already given away"))
    }
}

macro_rules! mutate_api_object_as {
    {$x:ty, $y:ident: $($p:pat=>$r:expr,$m:expr,$t:expr,)+} => (
        impl UseHandleAs<$x> for ResolvedHandle {
            #[allow(unreachable_code, unused_variables)]
            fn as_ref(&self) -> Result<&$x> {
                match self.ob.as_ref().expect("object ownership was already given away") {
                    $($p => Ok($r),)+
                    _ => inv_arg(format!("object does not support the {} interface", stringify!($y))),
                }
            }

            #[allow(unreachable_code, unused_variables)]
            fn as_mut(&mut self) -> Result<&mut $x> {
                match self.ob.as_mut().expect("object ownership was already given away") {
                    $($p => Ok($m),)+
                    _ => inv_arg(format!("object does not support the {} interface", stringify!($y))),
                }
            }

            #[allow(unreachable_code, unused_variables)]
            fn take(&mut self) -> Result<$x> {
                match self.ob.take().expect("object ownership was already given away") {
                    $($p => Ok($t),)+
                    x => {
                        self.ob.replace(x);
                        inv_arg(format!("object does not support the {} interface", stringify!($y)))
                    }
                }
            }
        }
    )
}

mutate_api_object_as! {ArbData, arb:
    APIObject::ArbData(x) => x, x, x,
    APIObject::ArbCmd(x) => x.data(), x.data_mut(), x.into(),
    APIObject::ArbCmdQueue(x) => {
        if let Some(x) = x.front() {
            x.data()
        } else {
            return inv_arg("empty command queue does not support arb interface");
        }
    }, {
        if let Some(x) = x.front_mut() {
            x.data_mut()
        } else {
            return inv_arg("empty command queue does not support arb interface");
        }
    }, {
        let mut x = x;
        if let Some(x) = x.pop_front() {
            x.into()
        } else {
            return inv_arg("empty command queue does not support arb interface");
        }
    },
    APIObject::Gate(x) => &x.data, &mut x.data, x.data,
    APIObject::QubitMeasurementResult(x) => &x.data, &mut x.data, x.data,
}

mutate_api_object_as! {ArbCmd, cmd:
    APIObject::ArbCmd(x) => x, x, x,
    APIObject::ArbCmdQueue(x) => {
        if let Some(x) = x.front() {
            x
        } else {
            return inv_arg("empty command queue does not support cmd interface");
        }
    }, {
        if let Some(x) = x.front_mut() {
            x
        } else {
            return inv_arg("empty command queue does not support cmd interface");
        }
    }, {
        let mut x = x;
        if let Some(x) = x.pop_front() {
            x
        } else {
            return inv_arg("empty command queue does not support cmd interface");
        }
    },
}

mutate_api_object_as! {ArbCmdQueue, cq:
    APIObject::ArbCmdQueue(x) => x, x, x,
}

mutate_api_object_as! {QubitReferenceSet, qbset:
    APIObject::QubitReferenceSet(x) => x, x, x,
}

mutate_api_object_as! {Gate, gate:
    APIObject::Gate(x) => x, x, x,
}

mutate_api_object_as! {QubitMeasurementResult, meas:
    APIObject::QubitMeasurementResult(x) => x, x, x,
}

mutate_api_object_as! {QubitMeasurementResultSet, mset:
    APIObject::QubitMeasurementResult(x) => {
        return inv_arg("handle does not support the mset interface");
    }, {
        return inv_arg("handle does not support the mset interface");
    }, {
        let mut s = QubitMeasurementResultSet::new();
        s.insert(x.qubit, x);
        s
    },
    APIObject::QubitMeasurementResultSet(x) => x, x, x,
}

mutate_api_object_as! {PluginProcessConfiguration, pcfg:
    APIObject::PluginProcessConfiguration(x) => x, x, x,
}

mutate_api_object_as! {PluginThreadConfiguration, tcfg:
    APIObject::PluginThreadConfiguration(x) => x, x, x,
}

mutate_api_object_as! {BoxedPluginConfiguration, xcfg:
    APIObject::PluginProcessConfiguration(x) => panic!(), panic!(), Box::new(x),
    APIObject::PluginThreadConfiguration(x) => panic!(), panic!(), Box::new(x),
}

mutate_api_object_as! {SimulatorConfiguration, scfg:
    APIObject::SimulatorConfiguration(x) => x, x, x,
}

mutate_api_object_as! {Simulator, sim:
    APIObject::Simulator(x) => x, x, x,
}

mutate_api_object_as! {PluginDefinition, scfg:
    APIObject::PluginDefinition(x) => x, x, x,
}

mutate_api_object_as! {PluginJoinHandle, pjoin:
    APIObject::PluginJoinHandle(x) => x, x, x,
}

/// Resolves an object from a handle.
///
/// This takes an object from the thread-local pool, so no reference is
/// maintained. The object is reinserted into the pool when the returned
/// container object is dropped, unless ownership of the object was taken
/// over.
pub fn resolve(handle: dqcs_handle_t) -> Result<ResolvedHandle> {
    if let Some(ob) = API_STATE.with(|state| state.borrow_mut().objects.remove(&handle)) {
        Ok(ResolvedHandle {
            ob: Some(ob),
            handle,
        })
    } else {
        inv_arg(format!("handle {} is invalid", handle))
    }
}

/// Resolves a handle into an underlying object.
///
/// This is a convenience macro for calling `resolve()` followed by `as_ref()`,
/// `as_mut()`, or `take()`. The following forms are available:
///
///  - `resolve!(x as &Type)`: converts `x` to `&Type` using let statements and
///    `as_ref()`.
///  - `resolve!(x as &mut Type)`: as above, but `as_mut()`.
///  - `resolve!(x as pending Type)`: only resolves the handle and ensures that
///    it CAN be downcast to `Type`, but doesn't perform the downcast yet. This
///    can be done later with `take!`. Reason being, that all possible errors
///    need to have been caught before `take()` is called, otherwise the object
///    cannot be placed back into the pool.
///
/// Note that this macro uses `?` to throw errors, so the callee must return
/// `Result<_>`.
#[doc(hidden)]
#[macro_export]
macro_rules! resolve {
    ($i:ident as &$t:ty) => {
        let $i = resolve($i)?;
        let $i: &$t = $i.as_ref()?;
    };
    ($i:ident as &mut $t:ty) => {
        let mut $i = resolve($i)?;
        let $i: &mut $t = $i.as_mut()?;
    };
    ($i:ident as pending $t:ty) => {
        let mut $i = resolve($i)?;
        let _: &mut $t = $i.as_mut()?;
    };
    (optional $i:ident as &$t:ty) => {
        let $i = resolve($i).ok();
        let $i: Option<&$t> = if let Some($i) = $i.as_ref() {
            Some($i.as_ref()?)
        } else {
            None
        };
    };
    (optional $i:ident as &mut $t:ty) => {
        let mut $i = resolve($i).ok();
        let $i: Option<&$t> = if let Some($i) = $i.as_mut() {
            Some($i.as_ref()?)
        } else {
            None
        };
    };
    (optional $i:ident as pending $t:ty) => {
        let mut $i = resolve($i).ok();
        if let Some($i) = $i.as_mut() {
            let _: &mut $t = $i.as_mut()?;
        }
    };
}

/// Takes ownership of an handle previously resolved using the
/// `resolve!(x as pending Type)` syntax.
///
/// The following forms are available:
///
///  - `take!(x as Type)`: converts `x` to `Type` using `take()`.
///  - `take!(resolved x as Type)`: as above, but `x` must have previously been
///    resolved using `resolve!(x as pending Type)`. This cannot fail.
///  - `take!(x as mut Type)`: converts `x` to `mut Type` using `take()`.
///  - `take!(resolved x as mut Type)`: as above, but `x` must have previously
///    been resolved using `resolve!(x as pending Type)`. This cannot fail.
///
/// This macro does not throw errors, but it can panic if the downcast fails.
/// The downcast should have been previously checked by the user through the
/// appropriate `resolve!` macro; this will only fail if the types specified
/// for `resolve!` and `take!` differ (don't do this).
#[doc(hidden)]
#[macro_export]
macro_rules! take {
    ($i:ident as $t:ty) => {
        let mut $i = resolve($i)?;
        let $i: $t = $i.take()?;
    };
    ($i:ident as mut $t:ty) => {
        let mut $i = resolve($i)?;
        let mut $i: $t = $i.take()?;
    };
    (resolved $i:ident as $t:ty) => {
        let $i: $t = $i.take().unwrap();
    };
    (resolved $i:ident as mut $t:ty) => {
        let mut $i: $t = $i.take().unwrap();
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! clone {
    ($clone:ident: $t:ty = resolved $i:ident) => {
        let $clone: &$t = $i.as_ref().unwrap();
        let $clone = $clone.clone();
    };
    (mut $clone:ident: $t:ty = resolved $i:ident) => {
        let $clone: &$t = $i.as_ref().unwrap();
        let mut $clone = $clone.clone();
    };
}

/// Deletes a handle.
///
/// This never returns an error, and "double deletes" are fine (the second
/// delete will be no-op).
#[doc(hidden)]
#[macro_export]
macro_rules! delete {
    ($i:ident) => {
        let $i = resolve($i);
        if let Ok(mut $i) = $i {
            let _: APIObject = $i.take().unwrap();
        }
    };
    (resolved $i:ident) => {
        let _: APIObject = $i.take().unwrap();
    };
}

/// Inserts an object into the thread-local pool.
///
/// The handle to the object is returned.
pub fn insert(ob: impl Into<APIObject>) -> dqcs_handle_t {
    API_STATE.with(|state| {
        let mut state = state.borrow_mut();
        state.push(ob.into())
    })
}