Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pennylane as qml
- from pennylane import numpy as np
- n_wires = 4
- graph = [(0, 1), (0, 3), (1, 2), (2, 3)]
- # unitary operator U_B with parameter beta
- def U_B(beta):
- for wire in range(n_wires):
- qml.RX(2 * beta, wires=wire)
- # unitary operator U_C with parameter gamma
- def U_C(gamma):
- for edge in graph:
- wire1 = edge[0]
- wire2 = edge[1]
- qml.CNOT(wires=[wire1, wire2])
- qml.RZ(gamma, wires=wire2)
- qml.CNOT(wires=[wire1, wire2])
- def comp_basis_measurement(wires):
- n_wires = len(wires)
- return qml.Hermitian(np.diag(range(2 ** n_wires)), wires=wires)
- dev = qml.device("default.qubit", wires=n_wires, analytic=True, shots=1)
- pauli_z = [[1, 0], [0, -1]]
- pauli_z_2 = np.kron(pauli_z, pauli_z)
- @qml.qnode(dev)
- def circuit(gammas, betas, edge=None, n_layers=1):
- # apply Hadamards to get the n qubit |+> state
- for wire in range(n_wires):
- qml.Hadamard(wires=wire)
- # p instances of unitary operators
- for i in range(n_layers):
- U_C(gammas[i])
- U_B(betas[i])
- if edge is None:
- # measurement phase
- return qml.sample(comp_basis_measurement(range(n_wires)))
- # during the optimization phase we are evaluating a term
- # in the objective using expval
- return qml.expval(qml.Hermitian(pauli_z_2, wires=edge))
- def qaoa_maxcut(n_layers=1):
- print("\np={:d}".format(n_layers))
- # initialize the parameters near zero
- init_params = np.array([[0.8,-0.3],[0.7,-1.4]])
- # minimize the negative of the objective function
- def objective(params):
- gammas = params[0]
- betas = params[1]
- neg_obj = 0
- for edge in graph:
- # objective for the MaxCut problem
- neg_obj -= 0.5 * (1 - circuit(gammas, betas, edge=edge, n_layers=n_layers))
- return neg_obj
- #opt = qml.AdagradOptimizer(0.1)
- opt = qml.RotosolveOptimizer()
- # optimize parameters in objective
- params = np.array([[ 1.56087298, -0.77972525], [ 0.40580708,-0.80786168]])
- steps = 2
- for i in range(steps):
- print("Objective before step {:5d}: {: .7f}".format(i, -objective(params)))
- print("Params are ", params)
- params = opt.step(objective, params)
- # sample measured bitstrings 100 times
- bit_strings = []
- n_samples = 100
- for i in range(0, n_samples):
- bit_strings.append(int(circuit(params[0], params[1], edge=None, n_layers=n_layers)))
- # print optimal parameters and most frequently sampled bitstring
- counts = np.bincount(np.array(bit_strings))
- most_freq_bit_string = np.argmax(counts)
- print("Optimized (gamma, beta) vectors:\n{}".format(params[:, :n_layers]))
- print("Most frequently sampled bit string is: {:04b}".format(most_freq_bit_string))
- return -objective(params), bit_strings
- # perform qaoa on our graph with p=1,2 and
- # keep the bitstring sample lists
- bitstrings2 = qaoa_maxcut(n_layers=2)[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement