Testing Models¶

Introduction¶

QInfer provides several premade models for quickly learning how to use the library, for making demonstrations, or to test new algorithms and approaches built on top of QInfer.

SimplePrecessionModel - Model of a single qubit undergoing Larmor precession¶

class qinfer.SimplePrecessionModel(min_freq=0)[source]

Bases: qinfer.test_models.SimpleInversionModel

Describes the free evolution of a single qubit prepared in the $$\left|+\right\rangle$$ state under a Hamiltonian $$H = \omega \sigma_z / 2$$, as explored in [GFWC12].

Parameters: Model Parameters: min_freq (float) – Minimum value for $$\omega$$ to accept as valid. This is used for testing techniques that mitigate the effects of degenerate models; there is no “good” reason to ever set this to be less than zero, other than to test with an explicitly broken model. omega – The precession frequency $$\omega$$. The evolution time $$t$$.
expparams_dtype
likelihood(outcomes, modelparams, expparams)[source]
score(outcomes, modelparams, expparams, return_L=False)[source]

UnknownT2Model - Model of a single qubit undergoing Larmor precession with finite decoherence¶

class qinfer.UnknownT2Model(allow_identical_outcomes=False, outcome_warning_threshold=0.99, n_outcomes_cutoff=None)[source]

Bases: qinfer.abstract_model.FiniteOutcomeModel

Describes the free evolution of a single qubit prepared in the :math:left|+

ight angle state under a Hamiltonian

$$H = \omega \sigma_z / 2$$ with an unknown $$T_2$$ process, as explored in [GFWC12].

modelparam omega:
The precession frequency $$\omega$$.
modelparam T2_inv:
The decoherence strength $$T_2^{-1}$$.
scalar-expparam float:
The evolution time $$t$$.
n_modelparams
modelparam_names
expparams_dtype
n_outcomes(modelparams)[source]
are_models_valid(modelparams)[source]
likelihood(outcomes, modelparams, expparams)[source]

NoisyCoinModel - Classical coin flip model corrupted by a noisy process¶

class qinfer.NoisyCoinModel[source]

Bases: qinfer.abstract_model.FiniteOutcomeModel

Implements the “noisy coin” model of [FB12], where the model parameter $$p$$ is the probability of the noisy coin. This model has two experiment parameters, $$\alpha$$ and $$\beta$$, which are the probabilities of observing a “0” outcome conditoned on the “true” outcome being 0 and 1, respectively. That is, for an ideal coin, $$\alpha = 1$$ and $$\beta = 0$$.

Note that $$\alpha$$ and $$\beta$$ are implemented as experiment parameters not because we expect to design over those values, but because a specification of each is necessary to honestly describe an experiment that was performed.

Model Parameters:

p – “Heads” probability $$p$$.

Experiment Parameters:

• alpha (float) – Visibility parameter $$\alpha$$.
• beta (float) – Visibility parameter $$\beta$$.
n_modelparams
expparams_dtype
is_n_outcomes_constant
static are_models_valid(modelparams)[source]
n_outcomes(expparams)[source]
likelihood(outcomes, modelparams, expparams)[source]

NDieModel¶

class qinfer.NDieModel(n=6, threshold=1e-07)[source]

Bases: qinfer.abstract_model.FiniteOutcomeModel

Implements a model of rolling a die with n sides, whose unknown model parameters are the weights of each side; a generalization of CoinModel. An experiment consists of rolling the die once. The faces of the die are zero indexed, labeled 0,1,2,…,n-1.

Parameters: n (int) – Number of sides on the die. threshold (float) – How close to 1 the probabilites of the sides of the die must be.
n_modelparams
expparams_dtype
is_n_outcomes_constant

Returns True if and only if the number of outcomes for each experiment is independent of the experiment being performed.

This property is assumed by inference engines to be constant for the lifetime of a Model instance.

are_models_valid(modelparams)[source]
n_outcomes(expparams)[source]

Returns an array of dtype uint describing the number of outcomes for each experiment specified by expparams.

Parameters: expparams (numpy.ndarray) – Array of experimental parameters. This array must be of dtype agreeing with the expparams_dtype property.
likelihood(outcomes, modelparams, expparams)[source]

Custom Models¶

Writing custom models is standard practice for QInfer users. See Implementing Custom Simulators and Models.

test_model() - Method to run suite of tests on a model instance¶

qinfer.tests.base_test.test_model(model, prior, expparams, stream=<open file '<stderr>', mode 'w'>)[source]

Tests the given Simulatable instance for errors. Useful for debugging new or third party models.

Parameters: model – Instance of Simulatable or a subclass thereof. prior – Instance of Distribution, or any other class which implements a function sample that returns valid modelparams. expparams – np.ndarray of experimental parameters to test with. stream – Stream to dump the results into, default is stderr.