polyadicqml.qiskit¶
Builders¶
polyadicqml.qiskit.qkBuilder¶
-
class
polyadicqml.qiskit.
qkBuilder
(nbqbits, *args, **kwargs)¶ Qiskit-circuits builder using rx, rz and cz gates.
- Parameters
nbqbits (int) – Number of qubits.
-
alldiam
(idx=None)¶ Add X-rotation of
.
- Parameters
idx (iterable, optional) – Indices on which to apply the rotation, by default
None
. IfNone
, apply to all qubits.- Returns
self
- Return type
- Raises
ValueError – If idx is out of range
TypeError – If the indices are not int
-
allin
(theta)¶ Add input gate to all qubits and input theta.
- Parameters
theta (list-like) – Parameters to input.
- Returns
self
- Return type
-
circuit
()¶ Return the built circuit.
- Returns
Built circuit
- Return type
qiskit.QuantumCircuit
-
cz
(a, b)¶ Add CZ gate between qubits a and b
- Parameters
a (int) – Control qubit
b (int) – Target qubit.
- Returns
self
- Return type
- Raises
ValueError – If a or b are out of range
TypeError – If the indices are not int
-
input
(idx, theta)¶ Add input gate.
It correspond to a rotation of
.
- Parameters
idx (Union[iterable, int]) – Index[-ices] of qubits on which to input theta.
theta (Union[list-like, float]) – Parameter[s] to input. Has to have the same length as
idx
.
- Returns
self
- Return type
- Raises
ValueError – If idx is out of range
TypeError – If the indices are not int
-
measure_all
()¶ Add measurement.
- Returns
self
- Return type
polyadicqml.qiskit.qkParallelBuilder¶
-
class
polyadicqml.qiskit.
qkParallelBuilder
(nbqbits, tot_nbqbits=None, *args, **kwargs)¶ Qiskit-circuits builder for running parallel on same QPU. Uses rx, rz and cz gates.
- Parameters
nbqbits (int) – Number of qubits.
tot_nbqbits (int, optional) – Number if qubits in the QPU, by default None. If None, this is equivalent to
qkBuilder
Circuits¶
polyadicqml.qiskit.qkCircuitML¶
-
class
polyadicqml.qiskit.
qkCircuitML
(make_circuit, nbqbits, nbparams, backend, cbuilder=<class 'polyadicqml.qiskit.qkBuilder.qkBuilder'>, noise_model=None, coupling_map=None, noise_backend=None, save_path=None)¶ Quantum ML circuit interface for qiskit and IBMQ. Provides a unified interface to run multiple parametric circuits with different input and model parameters.
- Parameters
make_circuit (callable of signature self.make_circuit) – Function to generate the circuit corresponding to input x and params.
nbqbits (int) – Number of qubits.
nbparams (int) – Number of parameters.
backend (Union[Backends, list, qiskit.providers.BaseBackend]) – Backend(s) on which to run the circuits
cbuilder (circuitBuilder, optional) – Circuit builder, by default
qkBuilder
noise_model (Union[list, qiskit.providers.aer.noise.NoiseModel], optional) – Noise model to be provided to the backend, by default
None
. Cannot be used with noise_backend.coupling_map (list, optional) – Coupling map to be provided to the backend, by default
None
. Cannot be used with noise_backend.noise_backend (Union[list, qiskit.providers.ibmq.IBMQBackend], optional) – IBMQ backend from which the noise model should be generated, by default
None
.save_path (str, optional) – Where to save the jobs outputs, by default
None
. Jobs are saved only if a path is specified
-
nbqbits
¶ Number of qubits.
- Type
int
-
nbparams
¶ Number of parameters.
- Type
int
- Raises
ValueError – If both noise_model and noise_backend are provided.
-
make_circuit_list
(X, params, nbshots=None)¶ Generate a circuit for each sample in X rows, with parameters params.
- Parameters
X (array-like) – Input matrix, of shape (nb_samples, nb_features) or (nb_features,). In the latter case, nb_samples is 1.
params (vector-like) – Parameter vector.
nbshots (int, optional) – Number of nbshots, by default
None
- Returns
List of nb_samples circuits.
- Return type
list[qiskit.QuantumCircuit]
-
request
(X, params, nbshots=None)¶ Create circuits corresponding to samples in X and parameters params and send jobs to the backend for execution.
- Parameters
X (array-like) – Input matrix, of shape (nb_samples, nb_features) or (nb_features,). In the latter case, nb_samples is 1.
params (vector-like) – Parameter vector.
nbshots (int, optional) – Number of nbshots, by default
None
- Returns
Job instance derived from BaseJob and list of corresponding circuits.
- Return type
(qiskit.providers.BaseJob, list[qiskit.QuantumCircuit])
-
result
(job, qc_list, nbshots=None)¶ Retrieve job results and returns bitstring counts.
- Parameters
job (qiskit.providers.BaseJob) – Job instance.
qc_list (list[qiskit.circuit.QuantumCircuit]) – List of quantum circuits executed in job, of length nb_samples.
nbshots (int, optional) – Number of shots, by default
None
. IfNone
, raw counts are returned.
- Returns
Bitstring counts as an array of shape (nb_samples, 2**nbqbits), in the same order as qc_list.
- Return type
array
- Raises
QiskitError – If job status is cancelled or had an error.
-
run
(X, params, nbshots=None, job_size=None)¶ Run the circuit with input X and parameters params.
- Parameters
X (array-like) – Input matrix of shape (nb_samples, nb_features).
params (vector-like) – Parameter vector.
nbshots (int, optional) – Number of shots for the circuit run, by default
None
. IfNone
, uses the backend default.job_size (int, optional) – Maximum job size, to split the circuit runs, by default
None
. IfNone
, put all nb_samples in the same job.
- Returns
Bitstring counts as an array of shape (nb_samples, 2**nbqbits)
- Return type
array
-
save_job
(job, save_path=None)¶ Save job output to json file.
- Parameters
job (qiskit.providers.BaseJob) – Job instance.
save_path (path, optional) – Where to save the output, by default
None
. If None, usesqkCircuitML.save_path
.
polyadicqml.qiskit.qkParallelML¶
-
class
polyadicqml.qiskit.
qkParallelML
(make_circuit, nbqbits, nbparams, backend, tot_nbqbits, cbuilder=<class 'polyadicqml.qiskit.qkBuilder.qkParallelBuilder'>, noise_model=None, noise_backend=None, save_path=None)¶ Quantum ML circuit interface for running two qkCircuitML circuits on parallel on the same QPU.
- Parameters
make_circuit (callable of signature self.make_circuit) – Function to generate the circuit corresponding to input x and params.
make_circuit – Function to generate the circuit corresponding to input x and params.
nbqbits (int) – Number of qubits of the circuit.
nbparams (int) – Number of parameters.
backend (Union[Backends, list, qiskit.providers.BaseBackend]) – Backend on which to run the circuits
tot_nbqbits (int) – Number of total qubits in the QPU. Has to be at least twice nbqbits.
cbuilder (circuitBuilder, optional) – Circuit builder, by default
qkParallelBuilder
noise_model (Union[list, qiskit.providers.aer.noise.NoiseModel], optional) – Noise model to be provided to the backend, by default
None
. Cannot be used with noise_backend.noise_backend (Union[list, qiskit.providers.ibmq.IBMQBackend], optional) – IBMQ backend from which the noise model should be generated, by default
None
.save_path (str, optional) – Where to save the jobs outputs, by default
None
. Jobs are saved only if a path is specified
-
nbqbits
¶ Number of qubits.
- Type
int
-
tot_nbqbits
¶ Number of tatal qubits in the QPU
- Type
int
-
nbparams
¶ Number of parameters.
- Type
int
- Raises
ValueError – If both noise_model and noise_backend are provided. If QPU size cannot contain two circuits.
-
make_circuit_list
(X, params, nbshots=None)¶ Generate a circuit for each sample in X rows, with parameters params.
- Parameters
X (array-like) – Input matrix, of shape (nb_samples, nb_features) or (nb_features,). In the latter case, nb_samples is 1.
params (vector-like) – Parameter vector.
nbshots (int, optional) – Number of nbshots, by default
None
- Returns
List of nb_samples circuits.
- Return type
list[qiskit.QuantumCircuit]
-
result
(job, qc_list, nbshots=None)¶ Retrieve job results and returns bitstring counts.
- Parameters
job (qiskit.providers.BaseJob) – Job instance.
qc_list (list[qiskit.circuit.QuantumCircuit]) – List of quantum circuits executed in job, of length nb_samples.
nbshots (int, optional) – Number of shots, by default
None
. IfNone
, raw counts are returned.
- Returns
Bitstring counts as an array of shape (nb_samples, 2**nbqbits), in the same order as qc_list.
- Return type
array
- Raises
QiskitError – If job status is cancelled or had an error.
-
run
(X, params, nbshots=None, job_size=None)¶ Run the circuit with input X and parameters params.
- Parameters
X (array-like) – Input matrix of shape (nb_samples, nb_features).
params (vector-like) – Parameter vector.
nbshots (int, optional) – Number of shots for the circuit run, by default
None
. IfNone
, uses the backend default.job_size (int, optional) – Maximum job size, to split the circuit runs, by default
None
. IfNone
, put all nb_samples in the same job.
- Returns
Bitstring counts as an array of shape (nb_samples, 2**nbqbits)
- Return type
array
polyadicqml.qiskit.utility¶
polyadicqml.qiskit.utility.Backends¶
-
class
polyadicqml.qiskit.utility.
Backends
(backend_name, noise_name=None, hub=None, group=None, project=None, repeat=1)¶ Utility class to load quiskit backends and iterate through them.
- Parameters
backend_name (Union[str, list[str]]) – Name of the desired backend[s]
noise_name (Union[str, list[str]], optional) – Name of the backend[s] to use as noise model, by default None. Only used if simulator is True
simulator (bool, optional) – Whether the backend is a simulator, by default False
hub (str, optional) – Name of the IBMQ hub, by default None
group (str, optional) – Name of the IBMQ group, by default None
project (str, optional) – Name of the IBMQ project, by default None
repeat (int, optional) – How many time to repeat each backend during iteration, by default 1
-
load_beckends
()¶ Load the desired backends and, if not yet logged, log in.