Import NumPy and Qiskit's main functionalities import numpy as np from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit from qiskit import execute from qiskit import IBMQ from qiskit.tools.visualization import plot\_histogram class Agent: def \_\_init\_\_(self, qnnet): self.qnnet = qnnet # agent's Quantum Neural Network def get\_backend(self, load\_accounts = True, # if accounts are to be loaded backend\_code = 'ibmq\_qasm\_simulator' # backend code ): # Load IBM account if needed if load\_accounts == True: IBMQ.load\_accounts() # Get the backend to use in the computation backend\_used = IBMQ.get\_backend(backend\_code) # If one is not using the QASM simulator get the backend status if backend\_code! = 'ibmq\_qasm\_simulator': print(backend\_used.status()) # Return the backend used return backend\_used def choose\_action(self, distributions, # probability distributions rewards, # reward system backend\_used, # backed to be used num\_shots): # number of shots to run in quantum computer # Step 0: get the expected values and unitary parameters: # Get the expected values expected\_values = np.dot(distributions,rewards) # Get the number of actions involved num\_actions = len(expected\_values) # Get the base number that we will need for the network size dim = int.(np.log2(num\_actions)) # Get the parameters for the adaptive gate maxstring = np.binary\_repr(np.argmax(expected\_values), width = dim) # Step 1: Setup the Quantum Artificial Neural Network: # Get the number of quantum registers q = QuantumRegister(3\*dim) # Get the number of classical registers c = ClassicalRegister(dim) # Setup the quantum neural network self.qnnet = QuantumCircuit(q, c) # Step 2: Implement the Reinforcement Learning Algorithm: # Exploration Stage for i in range(0,dim): self.qnnet.h(q[i]) for j in range(0,dim): self.qnnet.u3(float(maxstring[j])\*np.pi,0,0,q[dim+j]) for k in range(0,dim):

Quantum Neural Machine Learning: Theory and Experiments DOI: http://dx.doi.org/10.5772/intechopen.84149

A. Python Code for Quantum Neural Reinforcement Learning Problem.

from qiskit import ClassicalRegister, QuantumRegister, QuantumCircuit
