hqm.utils.aiinterface
1import pennylane as qml 2import typing 3 4class AIInterface: 5 ''' 6 This class implements the inteface between quantum circuits implemented in PennyLane and the two most popular DL libraries, 7 Pytorch and Tensorflow. 8 ''' 9 10 @staticmethod 11 def network_layer(circuit : qml.qnode, weight_shape : dict, n_qubits : int, aiframework : str) -> typing.Union[qml.qnn.TorchLayer, qml.qnn.KerasLayer]: 12 ''' 13 Static methods that embedd quantum layer into a torch or a keras layer. 14 15 Parameters: 16 ----------- 17 - circuit : qml.qnode 18 pennylane circuit to be embedded 19 - weight_shape : dict 20 shape of the trainalbe weights, it is derived from hqm.circuits.circuit.QuantumCircuit 21 - n_qubits : int 22 integer representing the number of qubits used for the circuit 23 - aiframeworkks : str 24 string representing which in wich ai framework the circuit will be embedded, can be 'torch' or 'keras' 25 26 Returns: 27 -------- 28 - qlayer : qml.qnn.TorchLayer or qml.qnn.KerasLayer 29 ''' 30 31 if aiframework not in ['torch', 'keras']: raise Exception(f"Accepted values for framerwork are 'torch' or 'keras', found {aiframework}") 32 if aiframework == "torch": qlayer = qml.qnn.TorchLayer(circuit, weight_shape) 33 elif aiframework == "keras": qlayer = qml.qnn.KerasLayer(circuit, weight_shape, output_dim=n_qubits) 34 else: raise Exception(f"Framerwork can be only torch or keras, found {aiframework}!") 35 36 return qlayer
class
AIInterface:
5class AIInterface: 6 ''' 7 This class implements the inteface between quantum circuits implemented in PennyLane and the two most popular DL libraries, 8 Pytorch and Tensorflow. 9 ''' 10 11 @staticmethod 12 def network_layer(circuit : qml.qnode, weight_shape : dict, n_qubits : int, aiframework : str) -> typing.Union[qml.qnn.TorchLayer, qml.qnn.KerasLayer]: 13 ''' 14 Static methods that embedd quantum layer into a torch or a keras layer. 15 16 Parameters: 17 ----------- 18 - circuit : qml.qnode 19 pennylane circuit to be embedded 20 - weight_shape : dict 21 shape of the trainalbe weights, it is derived from hqm.circuits.circuit.QuantumCircuit 22 - n_qubits : int 23 integer representing the number of qubits used for the circuit 24 - aiframeworkks : str 25 string representing which in wich ai framework the circuit will be embedded, can be 'torch' or 'keras' 26 27 Returns: 28 -------- 29 - qlayer : qml.qnn.TorchLayer or qml.qnn.KerasLayer 30 ''' 31 32 if aiframework not in ['torch', 'keras']: raise Exception(f"Accepted values for framerwork are 'torch' or 'keras', found {aiframework}") 33 if aiframework == "torch": qlayer = qml.qnn.TorchLayer(circuit, weight_shape) 34 elif aiframework == "keras": qlayer = qml.qnn.KerasLayer(circuit, weight_shape, output_dim=n_qubits) 35 else: raise Exception(f"Framerwork can be only torch or keras, found {aiframework}!") 36 37 return qlayer
This class implements the inteface between quantum circuits implemented in PennyLane and the two most popular DL libraries, Pytorch and Tensorflow.
@staticmethod
def
network_layer( circuit: <function <lambda>>, weight_shape: dict, n_qubits: int, aiframework: str) -> Union[pennylane.qnn.torch.TorchLayer, pennylane.qnn.keras.KerasLayer]:
11 @staticmethod 12 def network_layer(circuit : qml.qnode, weight_shape : dict, n_qubits : int, aiframework : str) -> typing.Union[qml.qnn.TorchLayer, qml.qnn.KerasLayer]: 13 ''' 14 Static methods that embedd quantum layer into a torch or a keras layer. 15 16 Parameters: 17 ----------- 18 - circuit : qml.qnode 19 pennylane circuit to be embedded 20 - weight_shape : dict 21 shape of the trainalbe weights, it is derived from hqm.circuits.circuit.QuantumCircuit 22 - n_qubits : int 23 integer representing the number of qubits used for the circuit 24 - aiframeworkks : str 25 string representing which in wich ai framework the circuit will be embedded, can be 'torch' or 'keras' 26 27 Returns: 28 -------- 29 - qlayer : qml.qnn.TorchLayer or qml.qnn.KerasLayer 30 ''' 31 32 if aiframework not in ['torch', 'keras']: raise Exception(f"Accepted values for framerwork are 'torch' or 'keras', found {aiframework}") 33 if aiframework == "torch": qlayer = qml.qnn.TorchLayer(circuit, weight_shape) 34 elif aiframework == "keras": qlayer = qml.qnn.KerasLayer(circuit, weight_shape, output_dim=n_qubits) 35 else: raise Exception(f"Framerwork can be only torch or keras, found {aiframework}!") 36 37 return qlayer
Static methods that embedd quantum layer into a torch or a keras layer.
Parameters:
- circuit : qml.qnode
pennylane circuit to be embedded - weight_shape : dict
shape of the trainalbe weights, it is derived from hqm.circuits.circuit.QuantumCircuit - n_qubits : int
integer representing the number of qubits used for the circuit - aiframeworkks : str
string representing which in wich ai framework the circuit will be embedded, can be 'torch' or 'keras'
Returns:
- qlayer : qml.qnn.TorchLayer or qml.qnn.KerasLayer