NormalDistribution

class NormalDistribution(num_qubits, mu=None, sigma=None, bounds=None, upto_diag=False, name='P(X)')[source]

A circuit to encode a discretized normal distribution in qubit amplitudes.

The probability density function of the normal distribution is defined as

\[\mathbb{P}(X = x) = \frac{1}{\sqrt{2\pi\sigma^2}} e^{-\frac{(x - \mu)^2}{\sigma^2}}\]

Note

The parameter sigma in this class equals the variance, \(\sigma^2\) and not the standard deviation. This is for consistency with multivariate distributions, where the uppercase sigma, \(\Sigma\), is associated with the covariance.

This circuit considers the discretized version of the normal distribution on 2 ** num_qubits equidistant points, \(x_i\), truncated to bounds. For a one-dimensional random variable, meaning num_qubits is a single integer, it applies the operation

\[\mathcal{P}_X |0\rangle^n = \sum_{i=0}^{2^n - 1} \sqrt{\mathbb{P}(x_i)} |i\rangle\]

where \(n\) is num_qubits.

Note

The circuit loads the square root of the probabilities into the qubit amplitudes such that the sampling probability, which is the square of the amplitude, equals the probability of the distribution.

In the multi-dimensional case, the distribution is defined as

\[\mathbb{P}(X = x) = \frac{\Sigma^{-1}}{\sqrt{2\pi}} e^{-\frac{(x - \mu)^2}{\Sigma}}\]

where \(\Sigma\) is the covariance. To specify a multivariate normal distribution, num_qubits is a list of integers, each specifying how many qubits are used to discretize the respective dimension. The arguments mu and sigma in this case are a vector and square matrix. If for instance, num_qubits = [2, 3] then mu is a 2d vector and sigma is the \(2 \times 2\) covariance matrix. The first dimension is discretized using 2 qubits, hence on 4 points, and the second dimension on 3 qubits, hence 8 points. Therefore the random variable is discretized on \(4 \times 8 = 32\) points.

Since, in general, it is not yet known how to efficiently prepare the qubit amplitudes to represent a normal distribution, this class computes the expected amplitudes and then uses the QuantumCircuit.initialize method to construct the corresponding circuit.

This circuit is for example used in amplitude estimation applications, such as finance [1, 2], where customer demand or the return of a portfolio could be modelled using a normal distribution.

Examples

>>> circuit = NormalDistribution(3, mu=1, sigma=1, bounds=(0, 2))
>>> circuit.draw()
     ┌────────────────────────────────────────────────────────────────────────────┐
q_0: ┤0                                                                           ├
     │                                                                            │
q_1: ┤1 initialize(0.30391,0.3435,0.37271,0.38824,0.38824,0.37271,0.3435,0.30391) ├
     │                                                                            │
q_2: ┤2                                                                           ├
     └────────────────────────────────────────────────────────────────────────────┘
>>> mu = [1, 0.9]
>>> sigma = [[1, -0.2], [-0.2, 1]]
>>> circuit = NormalDistribution([2, 3], mu, sigma)
>>> circuit.num_qubits
5
>>> from qiskit import QuantumCircuit
>>> mu = [1, 0.9]
>>> sigma = [[1, -0.2], [-0.2, 1]]
>>> bounds = [(0, 1), (-1, 1)]
>>> p_x = NormalDistribution([2, 3], mu, sigma, bounds)
>>> circuit = QuantumCircuit(6)
>>> circuit.append(p_x, list(range(5)))
>>> for i in range(5):
...    circuit.cry(2 ** i, i, 5)
>>> circuit.draw()
     ┌───────┐
q_0: ┤0      ├────■─────────────────────────────────────────
     │       │    │
q_1: ┤1      ├────┼────────■────────────────────────────────
     │       │    │        │
q_2: ┤2 P(X) ├────┼────────┼────────■───────────────────────
     │       │    │        │        │
q_3: ┤3      ├────┼────────┼────────┼────────■──────────────
     │       │    │        │        │        │
q_4: ┤4      ├────┼────────┼────────┼────────┼────────■─────
     └───────┘┌───┴───┐┌───┴───┐┌───┴───┐┌───┴───┐┌───┴────┐
q_5: ─────────┤ RY(1) ├┤ RY(2) ├┤ RY(4) ├┤ RY(8) ├┤ RY(16) ├
              └───────┘└───────┘└───────┘└───────┘└────────┘

References

[1]: Gacon, J., Zoufal, C., & Woerner, S. (2020).

Quantum-Enhanced Simulation-Based Optimization. arXiv:2005.10780

[2]: Woerner, S., & Egger, D. J. (2018).

Quantum Risk Analysis. arXiv:1806.06893

Parameters
  • num_qubits (Union[int, List[int]]) – The number of qubits used to discretize the random variable. For a 1d random variable, num_qubits is an integer, for multiple dimensions a list of integers indicating the number of qubits to use in each dimension.

  • mu (Union[float, List[float], None]) – The parameter \(\mu\), which is the expected value of the distribution. Can be either a float for a 1d random variable or a list of floats for a higher dimensional random variable. Defaults to 0.

  • sigma (Union[float, List[float], None]) – The parameter \(\sigma^2\) or \(\Sigma\), which is the variance or covariance matrix. Default to the identity matrix of appropriate size.

  • bounds (Union[Tuple[float, float], List[Tuple[float, float]], None]) – The truncation bounds of the distribution as tuples. For multiple dimensions, bounds is a list of tuples [(low0, high0), (low1, high1), ...]. If None, the bounds are set to (-1, 1) for each dimension.

  • upto_diag (bool) – If True, load the square root of the probabilities up to multiplication with a diagonal for a more efficient circuit.

  • name (str) – The name of the circuit.

Attributes

NormalDistribution.ancillas

Returns a list of ancilla bits in the order that the registers were added.

NormalDistribution.bounds

Return the bounds of the probability distribution.

NormalDistribution.calibrations

Return calibration dictionary.

NormalDistribution.clbits

Returns a list of classical bits in the order that the registers were added.

NormalDistribution.data

Return the circuit data (instructions and context).

NormalDistribution.extension_lib

NormalDistribution.global_phase

Return the global phase of the circuit in radians.

NormalDistribution.header

NormalDistribution.instances

NormalDistribution.num_ancillas

Return the number of ancilla qubits.

NormalDistribution.num_clbits

Return number of classical bits.

NormalDistribution.num_parameters

Convenience function to get the number of parameter objects in the circuit.

NormalDistribution.num_qubits

Return number of qubits.

NormalDistribution.parameters

Convenience function to get the parameters defined in the parameter table.

NormalDistribution.prefix

NormalDistribution.probabilities

Return the sampling probabilities for the values.

NormalDistribution.qubits

Returns a list of quantum bits in the order that the registers were added.

NormalDistribution.values

Return the discretized points of the random variable.

Methods

NormalDistribution.__getitem__(item)

Return indexed operation.

NormalDistribution.__len__()

Return number of operations in circuit.

NormalDistribution.add_calibration(gate, …)

Register a low-level, custom pulse definition for the given gate.

NormalDistribution.add_register(*regs)

Add registers.

NormalDistribution.append(instruction[, …])

Append one or more instructions to the end of the circuit, modifying the circuit in place.

NormalDistribution.assign_parameters(param_dict)

Assign parameters to new parameters or values.

NormalDistribution.barrier(*qargs)

Apply Barrier.

NormalDistribution.bind_parameters(value_dict)

Assign numeric parameters to values yielding a new circuit.

NormalDistribution.cast(value, _type)

Best effort to cast value to type.

NormalDistribution.cbit_argument_conversion(…)

Converts several classical bit representations (such as indexes, range, etc.) into a list of classical bits.

NormalDistribution.ccx(control_qubit1, …)

Apply CCXGate.

NormalDistribution.ch(control_qubit, …[, …])

Apply CHGate.

NormalDistribution.cls_instances()

Return the current number of instances of this class, useful for auto naming.

NormalDistribution.cls_prefix()

Return the prefix to use for auto naming.

NormalDistribution.cnot(control_qubit, …)

Apply CXGate.

NormalDistribution.combine(rhs)

Append rhs to self if self contains compatible registers.

NormalDistribution.compose(other[, qubits, …])

Compose circuit with other circuit or instruction, optionally permuting wires.

NormalDistribution.control([…])

Control this circuit on num_ctrl_qubits qubits.

NormalDistribution.copy([name])

Copy the circuit.

NormalDistribution.count_ops()

Count each operation kind in the circuit.

NormalDistribution.cp(theta, control_qubit, …)

Apply CPhaseGate.

NormalDistribution.crx(theta, control_qubit, …)

Apply CRXGate.

NormalDistribution.cry(theta, control_qubit, …)

Apply CRYGate.

NormalDistribution.crz(theta, control_qubit, …)

Apply CRZGate.

NormalDistribution.cswap(control_qubit, …)

Apply CSwapGate.

NormalDistribution.csx(control_qubit, …[, …])

Apply CSXGate.

NormalDistribution.cu(theta, phi, lam, …)

Apply CUGate.

NormalDistribution.cu1(theta, control_qubit, …)

Apply CU1Gate.

NormalDistribution.cu3(theta, phi, lam, …)

Apply CU3Gate.

NormalDistribution.cx(control_qubit, …[, …])

Apply CXGate.

NormalDistribution.cy(control_qubit, …[, …])

Apply CYGate.

NormalDistribution.cz(control_qubit, …[, …])

Apply CZGate.

NormalDistribution.dcx(qubit1, qubit2)

Apply DCXGate.

NormalDistribution.decompose()

Call a decomposition pass on this circuit, to decompose one level (shallow decompose).

NormalDistribution.delay(duration[, qarg, unit])

Apply Delay.

NormalDistribution.depth()

Return circuit depth (i.e., length of critical path).

NormalDistribution.diag_gate(diag, qubit)

Deprecated version of QuantumCircuit.diagonal.

NormalDistribution.diagonal(diag, qubit)

Attach a diagonal gate to a circuit.

NormalDistribution.draw([output, scale, …])

Draw the quantum circuit.

NormalDistribution.extend(rhs)

Append QuantumCircuit to the right hand side if it contains compatible registers.

NormalDistribution.fredkin(control_qubit, …)

Apply CSwapGate.

NormalDistribution.from_qasm_file(path)

Take in a QASM file and generate a QuantumCircuit object.

NormalDistribution.from_qasm_str(qasm_str)

Take in a QASM string and generate a QuantumCircuit object.

NormalDistribution.h(qubit)

Apply HGate.

NormalDistribution.hamiltonian(operator, …)

Apply hamiltonian evolution to to qubits.

NormalDistribution.has_register(register)

Test if this circuit has the register r.

NormalDistribution.i(qubit)

Apply IGate.

NormalDistribution.id(qubit)

Apply IGate.

NormalDistribution.initialize(params, qubits)

Apply initialize to circuit.

NormalDistribution.inverse()

Invert (take adjoint of) this circuit.

NormalDistribution.iso(isometry, q_input, …)

Attach an arbitrary isometry from m to n qubits to a circuit.

NormalDistribution.isometry(isometry, …[, …])

Attach an arbitrary isometry from m to n qubits to a circuit.

NormalDistribution.iswap(qubit1, qubit2)

Apply iSwapGate.

NormalDistribution.mcmt(gate, …[, …])

Apply a multi-control, multi-target using a generic gate.

NormalDistribution.mcp(lam, control_qubits, …)

Apply MCPhaseGate.

NormalDistribution.mcrx(theta, q_controls, …)

Apply Multiple-Controlled X rotation gate

NormalDistribution.mcry(theta, q_controls, …)

Apply Multiple-Controlled Y rotation gate

NormalDistribution.mcrz(lam, q_controls, …)

Apply Multiple-Controlled Z rotation gate

NormalDistribution.mct(control_qubits, …)

Apply MCXGate.

NormalDistribution.mcu1(lam, control_qubits, …)

Apply MCU1Gate.

NormalDistribution.mcx(control_qubits, …)

Apply MCXGate.

NormalDistribution.measure(qubit, cbit)

Measure quantum bit into classical bit (tuples).

NormalDistribution.measure_active([inplace])

Adds measurement to all non-idle qubits.

NormalDistribution.measure_all([inplace])

Adds measurement to all qubits.

NormalDistribution.mirror()

DEPRECATED: use circuit.reverse_ops().

NormalDistribution.ms(theta, qubits)

Apply MSGate.

NormalDistribution.num_connected_components([…])

How many non-entangled subcircuits can the circuit be factored to.

NormalDistribution.num_nonlocal_gates()

Return number of non-local gates (i.e.

NormalDistribution.num_tensor_factors()

Computes the number of tensor factors in the unitary (quantum) part of the circuit only.

NormalDistribution.num_unitary_factors()

Computes the number of tensor factors in the unitary (quantum) part of the circuit only.

NormalDistribution.p(theta, qubit)

Apply PhaseGate.

NormalDistribution.power(power[, matrix_power])

Raise this circuit to the power of power.

NormalDistribution.qasm([formatted, filename])

Return OpenQASM string.

NormalDistribution.qbit_argument_conversion(…)

Converts several qubit representations (such as indexes, range, etc.) into a list of qubits.

NormalDistribution.qubit_duration(*qubits)

Return the duration between the start and stop time of the first and last instructions, excluding delays, over the supplied qubits.

NormalDistribution.qubit_start_time(*qubits)

Return the start time of the first instruction, excluding delays, over the supplied qubits.

NormalDistribution.qubit_stop_time(*qubits)

Return the stop time of the last instruction, excluding delays, over the supplied qubits.

NormalDistribution.r(theta, phi, qubit)

Apply RGate.

NormalDistribution.rcccx(control_qubit1, …)

Apply RC3XGate.

NormalDistribution.rccx(control_qubit1, …)

Apply RCCXGate.

NormalDistribution.remove_final_measurements([…])

Removes final measurement on all qubits if they are present.

NormalDistribution.repeat(reps)

Repeat this circuit reps times.

NormalDistribution.reset(qubit)

Reset q.

NormalDistribution.reverse_bits()

Return a circuit with the opposite order of wires.

NormalDistribution.reverse_ops()

Reverse the circuit by reversing the order of instructions.

NormalDistribution.rx(theta, qubit[, label])

Apply RXGate.

NormalDistribution.rxx(theta, qubit1, qubit2)

Apply RXXGate.

NormalDistribution.ry(theta, qubit[, label])

Apply RYGate.

NormalDistribution.ryy(theta, qubit1, qubit2)

Apply RYYGate.

NormalDistribution.rz(phi, qubit)

Apply RZGate.

NormalDistribution.rzx(theta, qubit1, qubit2)

Apply RZXGate.

NormalDistribution.rzz(theta, qubit1, qubit2)

Apply RZZGate.

NormalDistribution.s(qubit)

Apply SGate.

NormalDistribution.sdg(qubit)

Apply SdgGate.

NormalDistribution.size()

Returns total number of gate operations in circuit.

NormalDistribution.snapshot(label[, …])

Take a statevector snapshot of the internal simulator representation.

NormalDistribution.snapshot_density_matrix(label)

Take a density matrix snapshot of simulator state.

NormalDistribution.snapshot_expectation_value(…)

Take a snapshot of expectation value <O> of an Operator.

NormalDistribution.snapshot_probabilities(…)

Take a probability snapshot of the simulator state.

NormalDistribution.snapshot_stabilizer(label)

Take a stabilizer snapshot of the simulator state.

NormalDistribution.snapshot_statevector(label)

Take a statevector snapshot of the simulator state.

NormalDistribution.squ(unitary_matrix, qubit)

Decompose an arbitrary 2*2 unitary into three rotation gates.

NormalDistribution.swap(qubit1, qubit2)

Apply SwapGate.

NormalDistribution.sx(qubit)

Apply SXGate.

NormalDistribution.sxdg(qubit)

Apply SXdgGate.

NormalDistribution.t(qubit)

Apply TGate.

NormalDistribution.tdg(qubit)

Apply TdgGate.

NormalDistribution.to_gate([parameter_map, …])

Create a Gate out of this circuit.

NormalDistribution.to_instruction([…])

Create an Instruction out of this circuit.

NormalDistribution.toffoli(control_qubit1, …)

Apply CCXGate.

NormalDistribution.u(theta, phi, lam, qubit)

Apply UGate.

NormalDistribution.u1(theta, qubit)

Apply U1Gate.

NormalDistribution.u2(phi, lam, qubit)

Apply U2Gate.

NormalDistribution.u3(theta, phi, lam, qubit)

Apply U3Gate.

NormalDistribution.uc(gate_list, q_controls, …)

Attach a uniformly controlled gates (also called multiplexed gates) to a circuit.

NormalDistribution.ucrx(angle_list, …)

Attach a uniformly controlled (also called multiplexed) Rx rotation gate to a circuit.

NormalDistribution.ucry(angle_list, …)

Attach a uniformly controlled (also called multiplexed) Ry rotation gate to a circuit.

NormalDistribution.ucrz(angle_list, …)

Attach a uniformly controlled (also called multiplexed gates) Rz rotation gate to a circuit.

NormalDistribution.unitary(obj, qubits[, label])

Apply unitary gate to q.

NormalDistribution.width()

Return number of qubits plus clbits in circuit.

NormalDistribution.x(qubit[, label])

Apply XGate.

NormalDistribution.y(qubit)

Apply YGate.

NormalDistribution.z(qubit)

Apply ZGate.

NormalDistribution.__getitem__(item)

Return indexed operation.

NormalDistribution.__len__()

Return number of operations in circuit.