def qaoa_maxcut(n_layers=1): print("\np={:d}".format(n_layers)) # initialize the parameters near zero init_params = 0.01 * np.random.rand(2, 2) # 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 # initialize optimizer: Adagrad works well empirically opt = qml.AdagradOptimizer(stepsize=0.5) # optimize parameters in objective params = init_params steps = 30 for i in range(steps): params = opt.step(objective, params) if (i + 1) % 5 == 0: print("Objective after step {:5d}: {: .7f}".format( i + 1, -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
def get_counts(params): gammas = [params[0], params[2], params[4], params[6]] betas = [params[1], params[3], params[5], params[7]] # The results (bit strings) of running the circuit 100 times and getting 100 measurements bit_strings = [] for i in range(0, num_reps): hold = int( circuit(gammas, betas, edge=None, num_layers=num_layers)) bit_strings.append( hold ) # This appends the integer from 0-15 (if 4 nodes) so it outputs the computational basis measurement in decimal. counts = np.bincount( np.array(bit_strings) ) # A 1x16 array that shows the frequency of each bitstring output most_freq_bit_string = np.argmax( counts) # Finds the most frequent bitstring return counts, bit_strings, most_freq_bit_string
# We assume that the system is measured in the computational basis. # If we label each basis state with a decimal integer j = 0, 1, ... 2 ** n_qubits - 1, # this is equivalent to a measurement of the following diagonal observable. basis_obs = qml.Hermitian(np.diag(range(2**n_qubits)), wires=range(n_qubits)) return qml.sample(basis_obs) ############################################################################## # To estimate the probability distribution over the basis states we first take ``n_shots`` # samples and then compute the relative frequency of each outcome. samples = prepare_and_sample(w).astype(int) q_probs = np.bincount(samples, minlength=2**n_qubits) / n_shots ############################################################################## # Comparison # ^^^^^^^^^^ # # Let us print the classical result. print("x_n^2 =\n", c_probs) ############################################################################## # The previous probabilities should match the following quantum state probabilities. print("|<x|n>|^2=\n", q_probs) ############################################################################## # Let us graphically visualize both distributions.
# We assume that the system is measured in the computational basis. # If we label each basis state with a decimal integer j = 0, 1, ... 2 ** n_qubits - 1, # this is equivalent to a measurement of the following diagonal observable. basis_obs = qml.Hermitian(np.diag(range(2**n_qubits)), wires=range(n_qubits)) return qml.sample(basis_obs) ############################################################################## # To estimate the probability distribution over the basis states we first take ``n_shots`` # samples and then compute the relative frequency of each outcome. samples = prepare_and_sample(w).astype(int) q_probs = np.bincount(samples) / n_shots ############################################################################## # Comparison # ^^^^^^^^^^ # # Let us print the classical result. print("x_n^2 =\n", c_probs) ############################################################################## # The previous probabilities should match the following quantum state probabilities. print("|<x|n>|^2=\n", q_probs) ############################################################################## # Let us graphically visualize both distributions.