qiboml.models#

Submodules#

qiboml.models.encoding#

class qiboml.models.encoding.QuantumEncoding(nqubits: int, qubits: Optional[tuple[int]] = None, density_matrix: Optional[bool] = False, _circuit: Circuit = None)[source]#

Bases: ABC

Abstract Encoder class.

Parameters:
  • nqubits (int) – total number of qubits.

  • qubits (tuple[int], optional) – set of qubits it acts on, by default range(nqubits).

  • density_matrix (bool, optional) – whether to build the circuit with density_matrix=True, mostly useful for noisy simulations. density_matrix=False by default.

nqubits: int#
qubits: Optional[tuple[int]] = None#
density_matrix: Optional[bool] = False#
_circuit: Circuit = None#
property _data_to_gate#

Mapping between the index of the input and the indices of the gates in the produced encoding circuit queue, where the input is encoded to. For instance, {0: [0,2], 1: [2]}, represents an encoding where the element 0 of the inputs enters the gates with indices 0 and 2 of the queue, whereas the element 1 of the input affects only the the gate in position 2 of the queue. By deafult, the map reproduces a simple encoding where the i-th component of the data is uploaded in the i-th gate of the queue.

abstract __call__(x: ndarray[Any, dtype[_ScalarType_co]]) Circuit[source]#

Abstract call method.

property circuit: Circuit#

Internal initialized circuit.

property differentiable: bool#

Whether the encoder is differentiable. If True the gradient w.r.t. the inputs is calculated, otherwise it is automatically set to zero.

_abc_impl = <_abc._abc_data object>#
class qiboml.models.encoding.PhaseEncoding(nqubits: int, qubits: Optional[tuple[int]] = None, density_matrix: Optional[bool] = False, _circuit: qibo.models.circuit.Circuit = None, encoding_gate: type = <factory>)[source]#

Bases: QuantumEncoding

encoding_gate: type#
__call__(x: ndarray[Any, dtype[_ScalarType_co]]) Circuit[source]#

Construct the circuit encoding the x data in the chosen encoding gate.

Parameters:

x (ndarray) – the input real data to encode in rotation angles.

Returns:

the constructed qibo.Circuit.

Return type:

(Circuit)

_abc_impl = <_abc._abc_data object>#
_circuit: Circuit = None#
property _data_to_gate#

Mapping between the index of the input and the indices of the gates in the produced encoding circuit queue, where the input is encoded to. For instance, {0: [0,2], 1: [2]}, represents an encoding where the element 0 of the inputs enters the gates with indices 0 and 2 of the queue, whereas the element 1 of the input affects only the the gate in position 2 of the queue. By deafult, the map reproduces a simple encoding where the i-th component of the data is uploaded in the i-th gate of the queue.

property circuit: Circuit#

Internal initialized circuit.

density_matrix: Optional[bool] = False#
property differentiable: bool#

Whether the encoder is differentiable. If True the gradient w.r.t. the inputs is calculated, otherwise it is automatically set to zero.

qubits: Optional[tuple[int]] = None#
nqubits: int#
class qiboml.models.encoding.BinaryEncoding(nqubits: int, qubits: Optional[tuple[int]] = None, density_matrix: Optional[bool] = False, _circuit: Circuit = None)[source]#

Bases: QuantumEncoding

__call__(x: ndarray[Any, dtype[_ScalarType_co]]) Circuit[source]#

Construct the circuit encoding the x binary data in some RX rotation gates with angles either \(\pi\) (for ones) or 0 (for zeros).

Parameters:

x (ndarray) – the input binary data.

Returns:

the constructed qibo.Circuit.

Return type:

(Circuit)

property differentiable: bool#

Whether the encoder is differentiable. If True the gradient w.r.t. the inputs is calculated, otherwise it is automatically set to zero.

_abc_impl = <_abc._abc_data object>#
_circuit: Circuit = None#
property _data_to_gate#

Mapping between the index of the input and the indices of the gates in the produced encoding circuit queue, where the input is encoded to. For instance, {0: [0,2], 1: [2]}, represents an encoding where the element 0 of the inputs enters the gates with indices 0 and 2 of the queue, whereas the element 1 of the input affects only the the gate in position 2 of the queue. By deafult, the map reproduces a simple encoding where the i-th component of the data is uploaded in the i-th gate of the queue.

property circuit: Circuit#

Internal initialized circuit.

density_matrix: Optional[bool] = False#
qubits: Optional[tuple[int]] = None#
nqubits: int#

qiboml.models.ansatze#

qiboml.models.ansatze.HardwareEfficient(nqubits: int, qubits: list[int] = None, nlayers: int = 1, density_matrix: Optional[bool] = False) Circuit[source]#

qiboml.models.decoding#

class qiboml.models.decoding.QuantumDecoding(nqubits: int, qubits: Optional[tuple[int]] = None, nshots: Optional[int] = None, backend: Optional[Backend] = None, transpiler: Optional[Passes] = None, _circuit: Circuit = None)[source]#

Bases: object

Abstract decoder class.

Parameters:
  • nqubits (int) – total number of qubits.

  • qubits (tuple[int], optional) – set of qubits it acts on, by default range(nqubits).

  • nshots (int, optional) – number of shots used for circuit execution and sampling.

  • backend (Backend, optional) – backend used for computation, by default the globally-set backend is used.

  • transpiler (Passes, optional) – transpiler to run before circuit execution, by default no transpilation is performed on the circuit (transpiler=None).

nqubits: int#
qubits: Optional[tuple[int]] = None#
nshots: Optional[int] = None#
backend: Optional[Backend] = None#
transpiler: Optional[Passes] = None#
_circuit: Circuit = None#
__call__(x: Circuit) Union[CircuitResult, QuantumState, MeasurementOutcomes][source]#

Combine the input circuir with the internal one and execute them with the internal backend.

Parameters:

x (Circuit) – input circuit.

Returns:

the execution qibo.result object.

Return type:

(CircuitResult | QuantumState | MeasurementOutcomes)

property circuit: Circuit#

A copy of the internal circuit.

Returns:

a copy of the internal circuit.

Return type:

(Circuit)

set_backend(backend: Backend)[source]#

Set the internal backend.

Parameters:

backend (Backend) – backend to be set.

property output_shape#

The shape of the decoded outputs.

property analytic: bool#

Whether the decoder is analytic, i.e. the gradient is ananlytically computable, or not (e.g. if sampling is involved).

Returns:

True if nshots is None, False otherwise.

Return type:

(bool)

class qiboml.models.decoding.Probabilities(nqubits: int, qubits: Optional[tuple[int]] = None, nshots: Optional[int] = None, backend: Optional[Backend] = None, transpiler: Optional[Passes] = None, _circuit: Circuit = None)[source]#

Bases: QuantumDecoding

The probabilities decoder.

__call__(x: Circuit) ndarray[Any, dtype[_ScalarType_co]][source]#

Computes the final state probabilities.

Parameters:

x (Circuit) – input circuit.

Returns:

the final probabilities.

Return type:

(ndarray)

property output_shape: tuple[int, int]#

Shape of the output probabilities.

Returns:

a (1, 2**nqubits) shape.

Return type:

(tuple[int, int])

property analytic: bool#

Whether the decoder is analytic, i.e. the gradient is ananlytically computable, or not (e.g. if sampling is involved).

Returns:

True if nshots is None, False otherwise.

Return type:

(bool)

_circuit: Circuit = None#
backend: Optional[Backend] = None#
property circuit: Circuit#

A copy of the internal circuit.

Returns:

a copy of the internal circuit.

Return type:

(Circuit)

nshots: Optional[int] = None#
qubits: Optional[tuple[int]] = None#
set_backend(backend: Backend)#

Set the internal backend.

Parameters:

backend (Backend) – backend to be set.

transpiler: Optional[Passes] = None#
nqubits: int#
class qiboml.models.decoding.Expectation(nqubits: int, qubits: Optional[tuple[int]] = None, nshots: Optional[int] = None, backend: Optional[Backend] = None, transpiler: Optional[Passes] = None, _circuit: Circuit = None, observable: Union[ndarray[Any, dtype[_ScalarType_co]], Hamiltonian] = None)[source]#

Bases: QuantumDecoding

The expectation value decoder.

Parameters:

observable (Hamiltonian | ndarray) – the observable to calculate the expectation value of,

by default \(Z_0\otimes Z_1\otimes ... \otimes Z_n\) is used.

observable: Union[ndarray[Any, dtype[_ScalarType_co]], Hamiltonian] = None#
__call__(x: Circuit) ndarray[Any, dtype[_ScalarType_co]][source]#

Execute the input circuit and calculate the expectation value of the internal observable on the final state

Parameters:

x (Circuit) – input Circuit.

Returns:

the calculated expectation value.

Return type:

(ndarray)

property output_shape: tuple[int, int]#

Shape of the output expectation value.

Returns:

a (1, 1) shape.

Return type:

(tuple[int, int])

set_backend(backend: Backend)[source]#

Set the internal and observable’s backends.

Parameters:

backend (Backend) – backend to be set.

_circuit: Circuit = None#
property analytic: bool#

Whether the decoder is analytic, i.e. the gradient is ananlytically computable, or not (e.g. if sampling is involved).

Returns:

True if nshots is None, False otherwise.

Return type:

(bool)

backend: Optional[Backend] = None#
property circuit: Circuit#

A copy of the internal circuit.

Returns:

a copy of the internal circuit.

Return type:

(Circuit)

nshots: Optional[int] = None#
qubits: Optional[tuple[int]] = None#
transpiler: Optional[Passes] = None#
nqubits: int#
class qiboml.models.decoding.State(nqubits: int, qubits: Optional[tuple[int]] = None, nshots: Optional[int] = None, backend: Optional[Backend] = None, transpiler: Optional[Passes] = None, _circuit: Circuit = None)[source]#

Bases: QuantumDecoding

The state decoder.

__call__(x: Circuit) ndarray[Any, dtype[_ScalarType_co]][source]#

Compute the final state of the input circuit and separates it in its real and imaginary parts stacked on top of each other.

Parameters:

x (Circuit) – input Circuit.

Returns:

the final state.

Return type:

(ndarray)

property output_shape: tuple[int, int, int]#

Shape of the output state.

Returns:

a (2, 1, 2**nqubits) shape.

Return type:

(tuple[int, int, int])

property analytic: bool#

Whether the decoder is analytic, i.e. the gradient is ananlytically computable, or not (e.g. if sampling is involved).

Returns:

True if nshots is None, False otherwise.

Return type:

(bool)

_circuit: Circuit = None#
backend: Optional[Backend] = None#
property circuit: Circuit#

A copy of the internal circuit.

Returns:

a copy of the internal circuit.

Return type:

(Circuit)

nshots: Optional[int] = None#
qubits: Optional[tuple[int]] = None#
set_backend(backend: Backend)#

Set the internal backend.

Parameters:

backend (Backend) – backend to be set.

transpiler: Optional[Passes] = None#
nqubits: int#
class qiboml.models.decoding.Samples(nqubits: int, qubits: Optional[tuple[int]] = None, nshots: Optional[int] = None, backend: Optional[Backend] = None, transpiler: Optional[Passes] = None, _circuit: Circuit = None)[source]#

Bases: QuantumDecoding

The samples decoder.

__call__(x: Circuit) ndarray[Any, dtype[_ScalarType_co]][source]#

Sample the final state of the circuit.

Parameters:

x (Circuit) – input Circuit.

Returns:

the generated samples.

Return type:

(ndarray)

property output_shape: tuple[int, int]#

Shape of the output samples.

Returns:

a (nshots, nqubits) shape.

Return type:

(tuple[int, int])

property analytic: bool#

Whether the decoder is analytic, i.e. the gradient is ananlytically computable, or not (e.g. if sampling is involved).

Returns:

True if nshots is None, False otherwise.

Return type:

(bool)

_circuit: Circuit = None#
backend: Optional[Backend] = None#
property circuit: Circuit#

A copy of the internal circuit.

Returns:

a copy of the internal circuit.

Return type:

(Circuit)

nshots: Optional[int] = None#
qubits: Optional[tuple[int]] = None#
set_backend(backend: Backend)#

Set the internal backend.

Parameters:

backend (Backend) – backend to be set.

transpiler: Optional[Passes] = None#
nqubits: int#