Quantum Layer
The Classiq engine exports theQLayer object, which inherits from torch.nn.Module (like most objects in the torch.nn namespace), and it acts like one.
The QLayer object is defined like this:
quantum_program, is the result of synthesizing a quantum model.
Note that the parameters are assumed to follow the API stated in qnn.
The second parameter is a callable which is responsible for executing the quantum program, usually with execute_qnn.
It takes a QuantumProgram and MultipleArguments (a list of arguments sets to assign to the quantum program parameters) as inputs, and returns a ResultsCollection.
Note that this argument can be left out, as demonstrated in the second code block.
If it is not supplied, the layer will create an ExecutionSession and sample the quantum program automagically.
note
note
In order to properly close the
ExecutionSession, if it was created, call the teardown method of QLayer.SavedResult as input, process it and returns a Tensor.
An example of such callables:
Execution
To facilitate the execution of your quantum layer, we supply the utility functionexecute_qnn.
It enables you to easily execute a batch of input arguments, and instruct whether you want the sample results or the estimation results according to a specific observable.
The inputs for execute_qnn are:
quantum_programof typeQuantumProgramargumentsof typeMultipleArguments- (optionally)
observableof typePauliOperator.
ResultsCollection, which is a list of SavedResult objects (see Execution Results for more information).
The type of each SavedResult depends on the observable input:
- If no
observablewere given, the type would beExecutionDetails. - Otherwise, the type would be
EstimationResult.
note
note
If only one observable was given,
execute_qnn will estimate the execution of all batched arguments with this observable.If more than one observable was given, their number should match the number of batched arguments, and each execution with a set of arguments will be estimated with the matching observable.Examples
Behind the Scenes
Behind the scenes, theQLayer handles the following actions:
- Processing of the PQC
- Initializing and tracking of parameters
- Passing the inputs and weights (as multi-dimensional tensors) to the execution function
- Passing the results from the execution function to the post-processing function
- Gradient calculation on the PQC