Qlbn#

Quantum-like Bayesian networks (Moreira and Wichert, 2014, 2016).

A discrete Bayesian network gives every full configuration \(x\) a classical probability \(P(x)\). The quantum-like network gives it an amplitude \(\sqrt{P(x)}\,e^{i\theta(x)}\). When unobserved (hidden) variables are marginalised, amplitudes rather than probabilities are summed:

\[P_q(\text{query} = v \mid e) = \alpha \,\Bigl|\sum_h \sqrt{P(v, e, h)}\,e^{i\theta_h}\Bigr|^2,\]

with \(\alpha\) normalising over the values \(v\). Interference terms are proportional to the cosine of the phase differences. With two hidden configurations a phase difference of \(\pi/2\) removes them and gives classical inference; with more configurations no choice of phases removes all of them in general, so the classical network is kept as a separate baseline.

Classes

BayesNet

Discrete Bayesian network.

QLBNModel

Fittable quantum-like Bayesian network.

ClassicalBNModel

Classical baseline for QLBNModel: the same network without interference.

Functions

classical_marginal

Classical posterior of one variable by enumeration.

quantum_like_marginal

Quantum-like posterior of one variable (amplitudes summed over hidden configurations).

n_hidden_configurations

Number of hidden configurations, i.e. of interference phases.

for_network

QLBNModel subclass with exactly the phases a network needs.

class BayesNet(variables, parents, cpt)[source]#

Bases: object

Discrete Bayesian network.

Parameters:
  • variables (dict) – {name: [values]}.

  • parents (dict) – {name: [parent names]} (variables without parents may be omitted).

  • cpt (dict) – Conditional tables, per variable either a function f(value, parent_values_dict) -> probability or nested dicts {tuple(parent values): {value: probability}}.

Variables:

order (list of str) – Variables in topological order (parents first).

prob(var, value, assignment)[source]#

Conditional probability of one variable.

Parameters:
  • var (str) – Variable name.

  • value (object) – Value of var.

  • assignment (dict) – Values of (at least) the parents of var.

Returns:

float – \(P(\text{var} = \text{value} \mid \text{parents})\).

joint(assignment)[source]#

Joint probability of a full assignment.

Parameters:

assignment (dict) – A value for every variable.

Returns:

float – Product of the conditional tables.

configurations(names)[source]#

Iterate over all assignments of some variables.

Parameters:

names (sequence of str) – Variables to enumerate.

Yields:

dict – {name: value}, in itertools.product() order.

class QLBNModel(**params)[source]#

Bases: Model

Fittable quantum-like Bayesian network.

The network is fixed; the parameters are the phases of the hidden configurations.

Parameters:
  • theta1 (float) – Phases \(\theta_1, \dots, \theta_{H-1}\) (\(\theta_0 = 0\)); at most 8 hidden configurations. Use for_network() to fit only the phases in use.

  • ... (float) – Phases \(\theta_1, \dots, \theta_{H-1}\) (\(\theta_0 = 0\)); at most 8 hidden configurations. Use for_network() to fit only the phases in use.

  • theta7 (float) – Phases \(\theta_1, \dots, \theta_{H-1}\) (\(\theta_0 = 0\)); at most 8 hidden configurations. Use for_network() to fit only the phases in use.

  • net (BayesNet) – The network (option).

  • query (str) – Query variable (option).

  • conditions (dict) – {condition name: evidence dict} (option). The design is a list of condition names.

predict(design=None)[source]#

Distribution of the query variable in each condition.

class ClassicalBNModel(**params)[source]#

Bases: Model

Classical baseline for QLBNModel: the same network without interference.

Takes the same options (net, query, conditions) and has no free parameters.

predict(design=None)[source]#

Distribution of the query variable in each condition.

classical_marginal(net, query, evidence=None)[source]#

Classical posterior of one variable by enumeration.

Parameters:
  • net (BayesNet) – The network.

  • query (str) – Query variable.

  • evidence (dict, optional) – Observed values {variable: value}.

Returns:

dict – {value: probability} of the query.

quantum_like_marginal(net, query, evidence=None, phases=None)[source]#

Quantum-like posterior of one variable (amplitudes summed over hidden configurations).

Parameters:
  • net (BayesNet) – The network.

  • query (str) – Query variable.

  • evidence (dict, optional) – Observed values.

  • phases (array_like, optional) – One phase per hidden configuration, in itertools.product() order of the hidden variables’ values. Default all zero.

Returns:

dict – {value: probability} of the query.

n_hidden_configurations(net, query, evidence=None)[source]#

Number of hidden configurations, i.e. of interference phases.

Parameters:
  • net (BayesNet) – The network.

  • query (str) – Query variable.

  • evidence (dict, optional) – Observed values.

Returns:

int

for_network(net, query, conditions)[source]#

QLBNModel subclass with exactly the phases a network needs.

Information criteria then count only the phases in use (the largest number of hidden configurations over the conditions, minus one).

Parameters:
  • net (BayesNet) – The network.

  • query (str) – Query variable.

  • conditions (dict) – {name: evidence}.

Returns:

type – Subclass of QLBNModel.

Raises:

ValueError – If more than 8 hidden configurations are needed.