qleet.interface package
Submodules
qleet.interface.circuit module
This module provides the interface to the circuits that the user specifies and library uses.
This makes the abstractions which ensures all operations in the library are backend agnostic, by allowing us to get the circuit back in the desired library’s form, cirq, qiskit or pytket. It allows the user to specify the circuit in any form. It also takes the loss function specification as a Pauli String.
It also exposes functions that the user can use to convert their circuits to a qiskit or cirq backend. WARNING: the conversion is done through a OpenQASM intermediate, operations not supported on QASM cannot be converted directly, please provide your circuit in a Cirq or Qiskit backend in that case.
- class qleet.interface.circuit.CircuitDescriptor(circuit: Union[qiskit.QuantumCircuit, cirq.Circuit, pyquil.Program], params: List[Union[sympy.Symbol, qiskit.circuit.Parameter]], cost_function: Optional[Union[cirq.PauliSum, qiskit.quantum_info.PauliList, pyquil.paulis.PauliSum]] = None)[source]
Bases:
object
The interface for users to provide a circuit in any framework and visualize it in qLEET.
It consists of 3 parts: * Circuit: which has the full ansatz preparation from the start where * Params: list of parameters which are used to parameterize the circuit * Cost Function: presently a pauli string, which we measure to get the
output we are optimizing over
Combined they form the full the parameterized quantum circuit from the initial qubits to the end measurement.
- property cirq_circuit: cirq.Circuit
Get the circuit in cirq :return: the cirq representation of the circuit :rtype: cirq.Circuit
- property cirq_cost: cirq.PauliSum
Returns the cost function, which is a function that takes in the state vector or the density matrix and returns the loss value of the solution envisioned by the Quantum Circuit. :raises ValueError: if the circuit is not from one of the supported frameworks :raises NotImplementedError: Long as qiskit and pyquil ports of pauli-string aren’t written :return: cost function TODO: Implement conversions into Cirq PauliSum
- property default_backend: str
Returns the backend in which the user had provided the circuit. :returns: The name of the default backend :rtype: str :raises ValueError: if the given circuit is not from a supported library
- classmethod from_qasm(qasm_str: str, params: List[Union[sympy.Symbol, qiskit.circuit.Parameter]], cost_function: Optional[Union[cirq.PauliSum, qiskit.quantum_info.PauliList, pyquil.paulis.PauliSum]], backend: str = 'cirq')[source]
Generate the descriptor from OpenQASM string
- Parameters
qasm_str (str) – OpenQASM string for each part of the circuit
params (list[sympy.Symbol]) – list of sympy symbols which act as parameters for the PQC
cost_function (PauliSum) – pauli-string operator to implement cost function
backend (str) – backend for the circuit descriptor objects
- Returns
The CircuitDescriptor object
- Return type
- property num_qubits: int
Get the number of qubits for a circuit :return: the number of qubits in the circuit :rtype: int :raises ValueError: if unsupported circuit framework is given
- property parameters: List[Union[sympy.Symbol, qiskit.circuit.Parameter]]
The list of sympy symbols to resolve as parameters, will be swept from 0 to 2*pi :return: list of parameters
- property pyquil_circuit: pyquil.Program
Get the circuit in pyquil :return: the pyquil representation of the circuit :rtype: pyquil.Program
- property qiskit_circuit: qiskit.QuantumCircuit
Get the circuit in qiskit :return: the qiskit representation of the circuit :rtype: qiskit.QuantumCircuit
- qleet.interface.circuit.convert_to_cirq(circuit: Union[qiskit.QuantumCircuit, cirq.Circuit, pyquil.Program]) cirq.Circuit [source]
Converts any circuit to cirq :type circuit: Circuit in any supported library :param circuit: input circuit in any framework :return: circuit in cirq :rtype: cirq.Circuit :raises ValueError: if the circuit is not from one of the supported frameworks
- qleet.interface.circuit.convert_to_pyquil(circuit: Union[qiskit.QuantumCircuit, cirq.Circuit, pyquil.Program]) qiskit.QuantumCircuit [source]
Converts any circuit to pyquil :type circuit: Circuit in any supported library :param circuit: input circuit in any framework :raises ValueError: if the circuit is not from one of the supported frameworks :return: circuit in pyquil :rtype: pyquil.Program
- qleet.interface.circuit.convert_to_qiskit(circuit: Union[qiskit.QuantumCircuit, cirq.Circuit, pyquil.Program]) qiskit.QuantumCircuit [source]
Converts any circuit to qiskit :type circuit: Circuit in any supported library :param circuit: input circuit in any framework :raises ValueError: if the circuit is not from one of the supported frameworks :return: circuit in qiskit :rtype: qiskit.QuantumCircuit
qleet.interface.dashboard module
qleet.interface.metas module
This module houses the interfaces for analyzers, and provide a utility container AnalyzerList
- MetaLogger is interface for those analyzers which need the state of the
circuit at each timestep in training.
- MetaExplorer is the interaface for those analyzers which can generate
properties from a single snapshot of the circuit.
- AnalyzerList is the convinience container which acts as a list of analyzers
which easy to use API.
- class qleet.interface.metas.AnalyzerList(*args: Union[MetaLogger, MetaExplorer])[source]
Bases:
object
Container class, Stores a list of loggers.
All the loggers can be asked to log the information they need together. The information to be logged can be provided to the Analyzer List in one convinient function call, and all the associated functions for all the loggers get called which can accept that form of data. All the loggers can also together be moved to the next model.
- log(solver: PQCSimulatedTrainer, loss: float) None [source]
Logs the current state of model in all the loggers. Does not ask the MetaAnalyzers to log the information since they don’t implement the logging interface. :type solver: PQCSimulatedTrainer :param solver: The PQC trainer whose parameters are to be logged :type loss: float :param loss: Loss value on the current epoch
- class qleet.interface.metas.MetaExplorer[source]
Bases:
ABC
Abstract class to represent interface of analyzing a the current state of the circuit. Treats the parameters of the circuit as a snapshot.
- class qleet.interface.metas.MetaLogger[source]
Bases:
ABC
Abstract class to represent interface of logging. Logs the present state of the model during training.
- abstract log(solver: PQCSimulatedTrainer, loss: float)[source]
Logs information at one timestep about either the solver or the present loss.
- Parameters
solver (PQCSimulatedTrainer) – The state of the PQC trainer at the current timestep
loss (float) – The loss at the current timestep
qleet.interface.metric_spec module
This module houses the ways to specify a metric, and sample solutions to compute it’s value.
Metrics are a useful abstraction which given the state of the circuit compute some classical value which we need to interpret or plot.
- class qleet.interface.metric_spec.MetricSpecifier(default_call_mode: str = 'samples')[source]
Bases:
ABC
Class to specify classical metrics which are a function of the sampled quantum state. Examples would be an arbitrary cost function, Mean Squared Error for some regression task, size of the max cut, etc.
This is an abstract class, all metrics should be it’s subclasses
How the metric is computed is left for the user to decide, it can be from the actual samples drawn from the circuit, or from the state vector, or from the density matrix.
- from_circuit(circuit_descriptor: CircuitDescriptor, parameters: Union[numpy.ndarray, List], mode: str = 'samples') float [source]
Computes the value of the metric from the circuit, by using the default mode or metric computation. :type circuit_descriptor: CircuitDescriptor :param circuit_descriptor: The provided circuit :type parameters: List or Numpy array :param parameters: List of values of the parameters to sample the circuit at :type mode: str :param mode: From what to compute the metric, samples, state_vector, or density_matrix :return: The value of the metric at those parameters :rtype: float :raises NotImplementedError: if required mode of evaluating metric wasn’t implemented :raises ValueError: if the mode specified wasn’t valid
- abstract from_density_matrix(density_matrix: numpy.ndarray) float [source]
Returns the value of the loss function given the density matrix of the state prepared from the circuit using the noise model provided. :type density_matrix: np.ndarray, 2-D of shape (2^n, 2^n) :param density_matrix: Vector of samples drawn from the circuit :return: value of the loss function :rtype: float
- abstract from_samples_vector(samples_vector: numpy.ndarray) float [source]
Returns the value of the loss function from one set of measurements sampled from the circuit. :type samples_vector: np.ndarray, 1-D of shape (n,) :param samples_vector: Vector of samples drawn from the circuit :return: value of the loss function :rtype: float
- abstract from_state_vector(state_vector: numpy.ndarray) float [source]
Returns the value of the loss function given the state vector of the state prepared from the circuit. :type state_vector: np.ndarray, 1-D of shape (2^n,) :param state_vector: State vector of state prepared by circuit :return: value of the loss function :rtype: float
- qleet.interface.metric_spec.sample_solutions(circuit: cirq.Circuit, param_symbols: List[sympy.Symbol], param_values: Iterable, samples: int = 1000) numpy.ndarray [source]
Get the computed cuts for a given ansatz :type circuit: cirq.Circuit :param circuit: Circuit to be sampled :type param_symbols: List of sympy.Symbols :param param_symbols: The symbols of model parameters :type param_values: List of floats :param param_values: The value of model parameters to sample at, 1-D vector :type samples: int :param samples: Number of times to sample the resulting quantum state :return: 2-D matrix, n_samples rows of boolean vectors showing the cut :rtype: np.array