hqm.noise.gaussianlike
1from types import FunctionType 2import pennylane as qml 3import numpy as np 4import sys 5 6sys.path += ['.', './ciruits/'] 7 8from hqm.circuits.circuit import QuantumCircuit 9 10class GaussianLikeNoiseGenerator(QuantumCircuit): 11 ''' 12 This class implements a qantum noise generator, with a distribution is 13 close to Gaussian. 14 ''' 15 16 def __init__(self, location : float, scale : float, dev : qml.devices = None) -> None: 17 ''' 18 GaussianLikeNoiseGenerator constructor. 19 20 Parameters: 21 ----------- 22 - location : float 23 location parameter translate the pdf, relative to the standard normal distribution 24 - scale : float 25 scale parameter that stretches the pdf. The greater the magnitude, the greater the stretching. 26 - dev : qml.device 27 PennyLane device on wich run quantum operations (dafault : None). When None it will be set 28 to 'default.qubit'. Recommendent qml.device("default.mixed", wires=1, shots=1000). 29 30 Returns: 31 -------- 32 Nothing, a GaussianLikeNoiseGenerator object will be created. 33 ''' 34 35 super().__init__(n_qubits=1, n_layers=1, dev=dev) 36 37 self.shots = dev.shots 38 self.location = location + 1/2 39 self.scale = scale * np.sqrt(self.shots/(1/2)) 40 self.circuit = self.circ(self.dev) 41 42 43 def generate_noise(self): 44 ''' 45 GaussianLikeNoiseGenerator that generates on observable 46 47 Parameters: 48 ----------- 49 Nothing 50 51 Returns: 52 -------- 53 n : float 54 gaussianlike quantum noise observable 55 ''' 56 counts = self.circuit() 57 n = self.scale*(counts[1]/self.shots-self.location) 58 return n 59 60 61 def generate_noise_array(self, shape:tuple): 62 ''' 63 GaussianLikeNoiseGenerator that generates a matrix of observables 64 65 Parameters: 66 ----------- 67 shape : tuple 68 shape of the desired maxtrix of observables 69 70 Returns: 71 -------- 72 n : float 73 gaussianlike quantum noise matrix of observables 74 ''' 75 n = [] 76 for _ in range(np.prod(shape)): n.append(self.generate_noise()) 77 n = np.array(n) 78 79 return n.reshape(shape) 80 81 def circ(self, dev : qml.devices) -> FunctionType: 82 ''' 83 GaussianLikeNoiseGenerator method that implements the quantum circuit. 84 85 Parameters: 86 ----------- 87 - dev : qml.device 88 PennyLane device on wich run quantum operations (dafault : None). When None it will be set 89 to 'default.qubit' 90 91 Returns: 92 -------- 93 - qnode : qml.qnode 94 the actual PennyLane circuit 95 ''' 96 97 @qml.qnode(dev) 98 def qnode() -> dict: 99 ''' 100 PennyLane based quantum circuit composed of a Ry gate 101 102 Parameters: 103 ----------- 104 Nothing 105 106 Returns: 107 -------- 108 - counts : dict 109 Sample from the supplied observable, with the number of shots determined from the dev.shots attribute of the corresponding device, 110 returning the number of counts for each sample. If no observable is provided then basis state samples are returned directly 111 from the device. Note that the output shape of this measurement process depends on the shots specified on the device. 112 ''' 113 114 qml.RY(np.pi/2, 0) 115 return qml.counts(qml.PauliZ(0)) 116 117 return qnode
class
GaussianLikeNoiseGenerator(typing.Generic[~quantumcircuit]):
11class GaussianLikeNoiseGenerator(QuantumCircuit): 12 ''' 13 This class implements a qantum noise generator, with a distribution is 14 close to Gaussian. 15 ''' 16 17 def __init__(self, location : float, scale : float, dev : qml.devices = None) -> None: 18 ''' 19 GaussianLikeNoiseGenerator constructor. 20 21 Parameters: 22 ----------- 23 - location : float 24 location parameter translate the pdf, relative to the standard normal distribution 25 - scale : float 26 scale parameter that stretches the pdf. The greater the magnitude, the greater the stretching. 27 - dev : qml.device 28 PennyLane device on wich run quantum operations (dafault : None). When None it will be set 29 to 'default.qubit'. Recommendent qml.device("default.mixed", wires=1, shots=1000). 30 31 Returns: 32 -------- 33 Nothing, a GaussianLikeNoiseGenerator object will be created. 34 ''' 35 36 super().__init__(n_qubits=1, n_layers=1, dev=dev) 37 38 self.shots = dev.shots 39 self.location = location + 1/2 40 self.scale = scale * np.sqrt(self.shots/(1/2)) 41 self.circuit = self.circ(self.dev) 42 43 44 def generate_noise(self): 45 ''' 46 GaussianLikeNoiseGenerator that generates on observable 47 48 Parameters: 49 ----------- 50 Nothing 51 52 Returns: 53 -------- 54 n : float 55 gaussianlike quantum noise observable 56 ''' 57 counts = self.circuit() 58 n = self.scale*(counts[1]/self.shots-self.location) 59 return n 60 61 62 def generate_noise_array(self, shape:tuple): 63 ''' 64 GaussianLikeNoiseGenerator that generates a matrix of observables 65 66 Parameters: 67 ----------- 68 shape : tuple 69 shape of the desired maxtrix of observables 70 71 Returns: 72 -------- 73 n : float 74 gaussianlike quantum noise matrix of observables 75 ''' 76 n = [] 77 for _ in range(np.prod(shape)): n.append(self.generate_noise()) 78 n = np.array(n) 79 80 return n.reshape(shape) 81 82 def circ(self, dev : qml.devices) -> FunctionType: 83 ''' 84 GaussianLikeNoiseGenerator method that implements the quantum circuit. 85 86 Parameters: 87 ----------- 88 - dev : qml.device 89 PennyLane device on wich run quantum operations (dafault : None). When None it will be set 90 to 'default.qubit' 91 92 Returns: 93 -------- 94 - qnode : qml.qnode 95 the actual PennyLane circuit 96 ''' 97 98 @qml.qnode(dev) 99 def qnode() -> dict: 100 ''' 101 PennyLane based quantum circuit composed of a Ry gate 102 103 Parameters: 104 ----------- 105 Nothing 106 107 Returns: 108 -------- 109 - counts : dict 110 Sample from the supplied observable, with the number of shots determined from the dev.shots attribute of the corresponding device, 111 returning the number of counts for each sample. If no observable is provided then basis state samples are returned directly 112 from the device. Note that the output shape of this measurement process depends on the shots specified on the device. 113 ''' 114 115 qml.RY(np.pi/2, 0) 116 return qml.counts(qml.PauliZ(0)) 117 118 return qnode
This class implements a qantum noise generator, with a distribution is close to Gaussian.
GaussianLikeNoiseGenerator( location: float, scale: float, dev: <module 'pennylane.devices' from '/Users/asebastianelli/miniforge3/envs/hqm/lib/python3.10/site-packages/pennylane/devices/__init__.py'> = None)
17 def __init__(self, location : float, scale : float, dev : qml.devices = None) -> None: 18 ''' 19 GaussianLikeNoiseGenerator constructor. 20 21 Parameters: 22 ----------- 23 - location : float 24 location parameter translate the pdf, relative to the standard normal distribution 25 - scale : float 26 scale parameter that stretches the pdf. The greater the magnitude, the greater the stretching. 27 - dev : qml.device 28 PennyLane device on wich run quantum operations (dafault : None). When None it will be set 29 to 'default.qubit'. Recommendent qml.device("default.mixed", wires=1, shots=1000). 30 31 Returns: 32 -------- 33 Nothing, a GaussianLikeNoiseGenerator object will be created. 34 ''' 35 36 super().__init__(n_qubits=1, n_layers=1, dev=dev) 37 38 self.shots = dev.shots 39 self.location = location + 1/2 40 self.scale = scale * np.sqrt(self.shots/(1/2)) 41 self.circuit = self.circ(self.dev)
GaussianLikeNoiseGenerator constructor.
Parameters:
- location : float
location parameter translate the pdf, relative to the standard normal distribution - scale : float
scale parameter that stretches the pdf. The greater the magnitude, the greater the stretching. - dev : qml.device
PennyLane device on wich run quantum operations (dafault : None). When None it will be set to 'default.qubit'. Recommendent qml.device("default.mixed", wires=1, shots=1000).
Returns:
Nothing, a GaussianLikeNoiseGenerator object will be created.
def
generate_noise(self):
44 def generate_noise(self): 45 ''' 46 GaussianLikeNoiseGenerator that generates on observable 47 48 Parameters: 49 ----------- 50 Nothing 51 52 Returns: 53 -------- 54 n : float 55 gaussianlike quantum noise observable 56 ''' 57 counts = self.circuit() 58 n = self.scale*(counts[1]/self.shots-self.location) 59 return n
GaussianLikeNoiseGenerator that generates on observable
Parameters:
Nothing
Returns:
n : float gaussianlike quantum noise observable
def
generate_noise_array(self, shape: tuple):
62 def generate_noise_array(self, shape:tuple): 63 ''' 64 GaussianLikeNoiseGenerator that generates a matrix of observables 65 66 Parameters: 67 ----------- 68 shape : tuple 69 shape of the desired maxtrix of observables 70 71 Returns: 72 -------- 73 n : float 74 gaussianlike quantum noise matrix of observables 75 ''' 76 n = [] 77 for _ in range(np.prod(shape)): n.append(self.generate_noise()) 78 n = np.array(n) 79 80 return n.reshape(shape)
GaussianLikeNoiseGenerator that generates a matrix of observables
Parameters:
shape : tuple shape of the desired maxtrix of observables
Returns:
n : float gaussianlike quantum noise matrix of observables
def
circ( self, dev: <module 'pennylane.devices' from '/Users/asebastianelli/miniforge3/envs/hqm/lib/python3.10/site-packages/pennylane/devices/__init__.py'>) -> function:
82 def circ(self, dev : qml.devices) -> FunctionType: 83 ''' 84 GaussianLikeNoiseGenerator method that implements the quantum circuit. 85 86 Parameters: 87 ----------- 88 - dev : qml.device 89 PennyLane device on wich run quantum operations (dafault : None). When None it will be set 90 to 'default.qubit' 91 92 Returns: 93 -------- 94 - qnode : qml.qnode 95 the actual PennyLane circuit 96 ''' 97 98 @qml.qnode(dev) 99 def qnode() -> dict: 100 ''' 101 PennyLane based quantum circuit composed of a Ry gate 102 103 Parameters: 104 ----------- 105 Nothing 106 107 Returns: 108 -------- 109 - counts : dict 110 Sample from the supplied observable, with the number of shots determined from the dev.shots attribute of the corresponding device, 111 returning the number of counts for each sample. If no observable is provided then basis state samples are returned directly 112 from the device. Note that the output shape of this measurement process depends on the shots specified on the device. 113 ''' 114 115 qml.RY(np.pi/2, 0) 116 return qml.counts(qml.PauliZ(0)) 117 118 return qnode
GaussianLikeNoiseGenerator method that implements the quantum circuit.
Parameters:
- dev : qml.device
PennyLane device on wich run quantum operations (dafault : None). When None it will be set
to 'default.qubit'
Returns:
- qnode : qml.qnode
the actual PennyLane circuit