QBayesian

class QBayesian(circuit, *, limit=10, threshold=0.9, sampler=None, pass_manager=None)[source]

Bases: object

Implements a quantum Bayesian inference (QBI) algorithm that has been developed in [1]. The Bayesian network must be based on binary random variables (0/1) and represented by a quantum circuit. The quantum circuit can be passed in various forms as long as it represents the joint probability distribution of the network.

For Bayesian networks with random variables that have more than two states, see for example [2].

Note that QBayesian defines an order for the qubits in the circuit. The last qubit in the circuit will correspond to the most significant bit in the joint probability distribution. For example, if the random variables A, B, and C are entered into the circuit in this order with (A=1, B=0 and C=0), the probability is represented by the probability amplitude of quantum state 001.

Example

qc = QuantumCircuit(...)

qb = QBayesian(qc)
result = qb.inference(query={...}, evidence={...})
print("Probability of query given evidence: ", result)
References

[1]: Low, Guang Hao, Theodore J. Yoder, and Isaac L. Chuang. “Quantum inference on Bayesian networks”, Physical Review A 89.6 (2014): 062315.

[2]: Borujeni, Sima E., et al. “Quantum circuit representation of Bayesian networks.” Expert Systems with Applications 176 (2021): 114768.

Parameters:
  • circuit (QuantumCircuit) – The quantum circuit that represents the Bayesian network. Each random variable should be assigned to exactly one register of one qubit. The last qubit in the circuit corresponds to the most significant bit in the binary string, which represents the measured quantum state.

  • limit (int) – The maximum number of times the Grover operator is integrated (2^limit).

  • threshold (float) – The threshold to accept the evidence. For example, if set to 0.9, this means that each evidence qubit must be equal to the value of the evidence variable at least 90% of the measurements.

  • sampler (BaseSampler | BaseSamplerV2 | None) – The sampler primitive used to compute the Bayesian inference. If None is given, a default instance of the reference sampler defined by Sampler will be used.

Raises:

ValueError – If any register in the circuit is not mapped to exactly one qubit.

Attributes

converged

Returns True if a solution for the evidence with the given threshold was found without reaching the maximum number of times the Grover operator was applied (2^limit).

limit

Returns the maximum number of times the Grover operator can be applied (2^limit).

sampler

Returns the sampler primitive used to compute the samples.

samples

Returns the samples generated from the rejection sampling.

threshold

Returns the threshold to accept the evidence.

Methods

inference(query, evidence=None)[source]

Performs quantum inference for the query variables given the evidence. It uses quantum rejection sampling if evidence is given and calculates the probability of the query.

Parameters:
  • query (Dict[str, int]) – The keys of the dictionary are the query variables that are linked to the corresponding quantum registers with their names and values (0/1). If the query variables are a real subset of all variables without the evidence, the query will be marginalized.

  • evidence (Dict[str, int] | None) – The evidence variables. If evidence is a dictionary, the rejection sampling is executed with the keys representing the variables linked to the corresponding quantum register by their names and values (0/1). If evidence is None, the default, then samples from the previous rejection sampling are used.

Returns:

The probability of the query given the evidence.

Raises:

ValueError – If evidence is required for rejection sampling and None is given.

Return type:

float

rejection_sampling(evidence, format_res=False)[source]

Performs quantum rejection sampling given the evidence.

Parameters:
  • evidence (Dict[str, int]) – The keys of the dictionary are the evidence variables that are linked to the corresponding quantum register with their names and values (0/1). If evidence is empty, it measures all qubits. If evidence is given, it uses the Grover operator for amplitude amplification and repeats until the evidence matches or limit is reached.

  • format_res (bool) – If true, maps the output back to variable names. For example, the output {‘100’: 0.23} with evidence A=0, B=0 will be mapped to {‘P(C=1|A=0,B=0)’: 0.23}.

Returns:

A dictionary with the probability distribution of the samples given the evidence, where the keys are the sequential values of the variables. Note that the last variable value appears as the first character for the key. If format_res is true, the output will be mapped back to the variable names, for example {‘P(C=1|A=0,B=0)’: 0.23}.

Return type:

Dict[str, float]