Quantum models
            QuantumModel(circuit, observable=None, backend=BackendName.PYQTORCH, diff_mode=DiffMode.AD, measurement=None, noise=None, mitigation=None, configuration=None)
  
            Bases: Module
The central class of qadence that executes QuantumCircuits and make them differentiable.
This class should be used as base class for any new quantum model supported in the qadence framework for information on the implementation of custom models see here.
Initialize a generic QuantumModel instance.
| PARAMETER | DESCRIPTION | 
|---|---|
circuit | 
          
             The circuit that is executed. 
                
                  TYPE:
                      | 
        
observable | 
          
             Optional observable(s) that are used only in the  
                
                  TYPE:
                      | 
        
backend | 
          
             A backend for circuit execution. 
                
                  TYPE:
                      | 
        
diff_mode | 
          
             A differentiability mode. Parameter shift based modes work on all backends. AD based modes only on PyTorch based backends. 
                
                  TYPE:
                      | 
        
measurement | 
          
             Optional measurement protocol. If None, use exact expectation value with a statevector simulator. 
                
                  TYPE:
                      | 
        
configuration | 
          
             Configuration for the backend. 
                
                  TYPE:
                      | 
        
noise | 
          
             A noise model to use. 
                
                  TYPE:
                      | 
        
| RAISES | DESCRIPTION | 
|---|---|
            
                ValueError
            
           | 
          
             if the   | 
        
Source code in qadence/models/quantum_model.py
                  
          in_features: int
  
  
      property
  
  Number of inputs.
          num_vparams: int
  
  
      property
  
  The number of variational parameters.
          out_features: int | None
  
  
      property
  
  Number of outputs.
          vals_vparams: Tensor
  
  
      property
  
  Dictionary with parameters which are actually updated during optimization.
          assign_parameters(values)
  Return the final, assigned circuit that is used in e.g. backend.run.
Source code in qadence/models/quantum_model.py
            
          
          expectation(values={}, observable=None, state=None, measurement=None, noise=None, mitigation=None, endianness=Endianness.BIG)
  Compute expectation using the given backend.
| RETURNS | DESCRIPTION | 
|---|---|
              
                  Tensor
              
           | 
          
             A torch.Tensor of shape n_batches x n_obs  | 
        
Source code in qadence/models/quantum_model.py
            
          reset_vparams(values)
  Reset all the variational parameters with a given list of values.
Source code in qadence/models/quantum_model.py
            
            QNN(circuit, observable, transform=None, backend=BackendName.PYQTORCH, diff_mode=DiffMode.AD, measurement=None, noise=None, configuration=None, inputs=None)
  
            Bases: QuantumModel
Quantum neural network model for n-dimensional inputs.
Examples:
import torch
from qadence import QuantumCircuit, QNN, Z
from qadence import hea, feature_map, hamiltonian_factory, kron
# create the circuit
n_qubits, depth = 2, 4
fm = kron(
    feature_map(1, support=(0,), param="x"),
    feature_map(1, support=(1,), param="y")
)
ansatz = hea(n_qubits=n_qubits, depth=depth)
circuit = QuantumCircuit(n_qubits, fm, ansatz)
obs_base = hamiltonian_factory(n_qubits, detuning=Z)
# the QNN will yield two outputs
obs = [2.0 * obs_base, 4.0 * obs_base]
# initialize and use the model
qnn = QNN(circuit, obs, inputs=["x", "y"])
y = qnn(torch.rand(3, 2))
Initialize the QNN.
The number of inputs is determined by the feature parameters in the input quantum circuit while the number of outputs is determined by how many observables are provided as input
| PARAMETER | DESCRIPTION | 
|---|---|
circuit | 
          
             The quantum circuit to use for the QNN. 
                
                  TYPE:
                      | 
        
transform | 
          
             A transformation applied to the output of the QNN. 
                
                  TYPE:
                      | 
        
inputs | 
          
             Tuple that indicates the order of variables of the tensors that are passed
to the model. Given input tensors  
                
                  TYPE:
                      | 
        
backend | 
          
             The chosen quantum backend. 
                
                  TYPE:
                      | 
        
diff_mode | 
          
             The differentiation engine to use. Choices 'gpsr' or 'ad'. 
                
                  TYPE:
                      | 
        
measurement | 
          
             optional measurement protocol. If None, use exact expectation value with a statevector simulator 
                
                  TYPE:
                      | 
        
noise | 
          
             A noise model to use. 
                
                  TYPE:
                      | 
        
configuration | 
          
             optional configuration for the backend 
                
                  TYPE:
                      | 
        
Source code in qadence/models/qnn.py
                  
          forward(values=None, state=None, measurement=None, noise=None, endianness=Endianness.BIG)
  Forward pass of the model.
This returns the (differentiable) expectation value of the given observable
operator defined in the constructor. Differently from the base QuantumModel
class, the QNN accepts also a tensor as input for the forward pass. The
tensor is expected to have shape: n_batches x in_features where n_batches
is the number of data points and in_features is the dimensionality of the problem
The output of the forward pass is the expectation value of the input
observable(s). If a single observable is given, the output shape is
n_batches while if multiple observables are given the output shape
is instead n_batches x n_observables
| PARAMETER | DESCRIPTION | 
|---|---|
values | 
          
             the values of the feature parameters 
                
                  TYPE:
                      | 
        
state | 
          
             Initial state. 
                
                  TYPE:
                      | 
        
measurement | 
          
             optional measurement protocol. If None, use exact expectation value with a statevector simulator 
                
                  TYPE:
                      | 
        
noise | 
          
             A noise model to use. 
                
                  TYPE:
                      | 
        
endianness | 
          
             Endianness of the resulting bit strings. 
                
                  TYPE:
                      | 
        
| RETURNS | DESCRIPTION | 
|---|---|
              Tensor
           | 
          
             a tensor with the expectation value of the observables passed in the constructor of the model 
                
                  TYPE:
                      |