Guest User

QAOA Maxcut Rotosolve Fail

a guest
Nov 2nd, 2020
51
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. # unitary operator U_B with parameter beta
  7. def U_B(beta):
  8.     for wire in range(n_wires):
  9.         qml.RX(2 * beta, wires=wire)
  10.  
  11.  
  12. # unitary operator U_C with parameter gamma
  13. def U_C(gamma):
  14.     for edge in graph:
  15.         wire1 = edge[0]
  16.         wire2 = edge[1]
  17.         qml.CNOT(wires=[wire1, wire2])
  18.         qml.RZ(gamma, wires=wire2)
  19.         qml.CNOT(wires=[wire1, wire2])
  20. def comp_basis_measurement(wires):
  21.     n_wires = len(wires)
  22.     return qml.Hermitian(np.diag(range(2 ** n_wires)), wires=wires)
  23.  
  24. dev = qml.device("default.qubit", wires=n_wires, analytic=True, shots=1)
  25.  
  26. pauli_z = [[1, 0], [0, -1]]
  27. pauli_z_2 = np.kron(pauli_z, pauli_z)
  28.  
  29.  
  30. @qml.qnode(dev)
  31. def circuit(gammas, betas, edge=None, n_layers=1):
  32.     # apply Hadamards to get the n qubit |+> state
  33.     for wire in range(n_wires):
  34.         qml.Hadamard(wires=wire)
  35.     # p instances of unitary operators
  36.     for i in range(n_layers):
  37.         U_C(gammas[i])
  38.         U_B(betas[i])
  39.     if edge is None:
  40.         # measurement phase
  41.         return qml.sample(comp_basis_measurement(range(n_wires)))
  42.     # during the optimization phase we are evaluating a term
  43.     # in the objective using expval
  44.     return qml.expval(qml.Hermitian(pauli_z_2, wires=edge))
  45.  
  46. def qaoa_maxcut(n_layers=1):
  47.     print("\np={:d}".format(n_layers))
  48.  
  49.     # initialize the parameters near zero
  50.     init_params = 0.01 * np.random.rand(2, 2)
  51.  
  52.     # minimize the negative of the objective function
  53.     def objective(params):
  54.         gammas = params[0]
  55.         betas = params[1]
  56.         neg_obj = 0
  57.         for edge in graph:
  58.             # objective for the MaxCut problem
  59.             neg_obj -= 0.5 * (1 - circuit(gammas, betas, edge=edge, n_layers=n_layers))
  60.         return neg_obj
  61. # CHANGED THIS TO ROTOSOLVE
  62.     opt = qml.RotosolveOptimizer()
  63.  
  64.     # optimize parameters in objective
  65.     params = init_params
  66.     steps = 30
  67.     for i in range(steps):
  68.         params = opt.step(objective, params)
  69.         if (i) % 5 == 0:
  70.             print("Objective after step {:5d}: {: .7f}".format(i, -objective(params)))
  71. # CHANGED THIS TO ADD PRINTING THE PARAMS INITIALLY
  72.             print("Params are ", 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. bitstrings1 = qaoa_maxcut(n_layers=1)[1]
  92. bitstrings2 = qaoa_maxcut(n_layers=2)[1]
RAW Paste Data