Advertisement
Guest User

QAOA Maxcut Rotosolve performing worse.

a guest
Nov 3rd, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.96 KB | None | 0 0
  1. import pennylane as qml
  2. from pennylane import numpy as np
  3. n_wires = 4
  4. graph = [(0, 1), (0, 3), (1, 2), (2, 3)]
  5.  
  6.  
  7. # unitary operator U_B with parameter beta
  8. def U_B(beta):
  9.     for wire in range(n_wires):
  10.         qml.RX(2 * beta, wires=wire)
  11.  
  12.  
  13. # unitary operator U_C with parameter gamma
  14. def U_C(gamma):
  15.     for edge in graph:
  16.         wire1 = edge[0]
  17.         wire2 = edge[1]
  18.         qml.CNOT(wires=[wire1, wire2])
  19.         qml.RZ(gamma, wires=wire2)
  20.         qml.CNOT(wires=[wire1, wire2])
  21. def comp_basis_measurement(wires):
  22.     n_wires = len(wires)
  23.     return qml.Hermitian(np.diag(range(2 ** n_wires)), wires=wires)
  24.  
  25. dev = qml.device("default.qubit", wires=n_wires, analytic=True, shots=1)
  26.  
  27. pauli_z = [[1, 0], [0, -1]]
  28. pauli_z_2 = np.kron(pauli_z, pauli_z)
  29.  
  30.  
  31. @qml.qnode(dev)
  32. def circuit(gammas, betas, edge=None, n_layers=1):
  33.     # apply Hadamards to get the n qubit |+> state
  34.     for wire in range(n_wires):
  35.         qml.Hadamard(wires=wire)
  36.     # p instances of unitary operators
  37.     for i in range(n_layers):
  38.         U_C(gammas[i])
  39.         U_B(betas[i])
  40.     if edge is None:
  41.         # measurement phase
  42.         return qml.sample(comp_basis_measurement(range(n_wires)))
  43.     # during the optimization phase we are evaluating a term
  44.     # in the objective using expval
  45.     return qml.expval(qml.Hermitian(pauli_z_2, wires=edge))
  46.  
  47. def qaoa_maxcut(n_layers=1):
  48.     print("\np={:d}".format(n_layers))
  49.  
  50.     # initialize the parameters near zero
  51.     init_params = np.array([[0.8,-0.3],[0.7,-1.4]])
  52.  
  53.  
  54.     # minimize the negative of the objective function
  55.     def objective(params):
  56.         gammas = params[0]
  57.         betas = params[1]
  58.         neg_obj = 0
  59.         for edge in graph:
  60.             # objective for the MaxCut problem
  61.             neg_obj -= 0.5 * (1 - circuit(gammas, betas, edge=edge, n_layers=n_layers))
  62.         return neg_obj
  63.  
  64.     #opt = qml.AdagradOptimizer(0.1)
  65.     opt = qml.RotosolveOptimizer()
  66.     # optimize parameters in objective
  67.     params = np.array([[ 1.56087298, -0.77972525], [ 0.40580708,-0.80786168]])
  68.     steps = 2
  69.     for i in range(steps):  
  70.         print("Objective before step {:5d}: {: .7f}".format(i, -objective(params)))
  71.         print("Params are ", params)
  72.         params = opt.step(objective, params)
  73.  
  74.     # sample measured bitstrings 100 times
  75.     bit_strings = []
  76.     n_samples = 100
  77.     for i in range(0, n_samples):
  78.         bit_strings.append(int(circuit(params[0], params[1], edge=None, n_layers=n_layers)))
  79.  
  80.     # print optimal parameters and most frequently sampled bitstring
  81.     counts = np.bincount(np.array(bit_strings))
  82.     most_freq_bit_string = np.argmax(counts)
  83.     print("Optimized (gamma, beta) vectors:\n{}".format(params[:, :n_layers]))
  84.     print("Most frequently sampled bit string is: {:04b}".format(most_freq_bit_string))
  85.  
  86.     return -objective(params), bit_strings
  87.  
  88.  
  89. # perform qaoa on our graph with p=1,2 and
  90. # keep the bitstring sample lists
  91. bitstrings2 = qaoa_maxcut(n_layers=2)[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement