Wire is not accessed

This code shows that wire is not accessed, but the result can be output. How can I change it?

# Put code here
import pennylane as qml
from pennylane import numpy as np
from matplotlib import pyplot as plt
np.random.seed(42)
n_wires = 6
G = 23.5
q = 3.0
# 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 wire in range(n_wires):
        wire0 = 0 
        wire1 = 1
        wire2 = 2
        wire3 = 3
        wire4 = 4
        wire5 = 5

        qml.CNOT(wires=[wire0, wire1])
        qml.RZ(2 * G * gamma, wires=wire1)
        qml.CNOT(wires=[wire0, wire1])

        qml.CNOT(wires=[wire1, wire2])
        qml.RZ(2 * G * gamma, wires=wire2)
        qml.CNOT(wires=[wire1, wire2])

        qml.CNOT(wires=[wire2, wire3])
        qml.RZ(2 * G * gamma, wires=wire3)
        qml.CNOT(wires=[wire2, wire3])

        qml.CNOT(wires=[wire3, wire4])
        qml.RZ(2 * G * gamma, wires=wire4)
        qml.CNOT(wires=[wire3, wire4])

        qml.CNOT(wires=[wire4, wire5])
        qml.RZ(2 * G * gamma, wires=wire5)
        qml.CNOT(wires=[wire4, wire5])

        qml.Barrier()
        qml.CNOT(wires=[wire0, wire2])
        qml.RZ(2 * G * gamma, wires=wire2)
        qml.CNOT(wires=[wire0, wire2])
        qml.CNOT(wires=[wire1, wire3])
        qml.RZ(2 * G * gamma, wires=wire3)
        qml.CNOT(wires=[wire1, wire3])

        qml.CNOT(wires=[wire2, wire4])
        qml.RZ(2 * G * gamma, wires=wire4)
        qml.CNOT(wires=[wire2, wire4])

        qml.CNOT(wires=[wire3, wire5])
        qml.RZ(2 * G * gamma, wires=wire5)
        qml.CNOT(wires=[wire3, wire5])

        qml.Barrier()

        qml.CNOT(wires=[wire0, wire3])
        qml.RZ(2 * G * gamma, wires=wire3)
        qml.CNOT(wires=[wire0, wire3])

        qml.CNOT(wires=[wire1, wire4])
        qml.RZ(2 * G * gamma, wires=wire4)
        qml.CNOT(wires=[wire1, wire4])

        qml.CNOT(wires=[wire2, wire5])
        qml.RZ(2 * G * gamma, wires=wire5)
        qml.CNOT(wires=[wire2, wire5])

        qml.Barrier()

        qml.CNOT(wires=[wire0, wire4])
        qml.RZ(2 * G * gamma, wires=wire4)
        qml.CNOT(wires=[wire0, wire4])
         qml.CNOT(wires=[wire1, wire5])
        qml.RZ(2 * G * gamma, wires=wire5)
        qml.CNOT(wires=[wire1, wire5])

        qml.Barrier()

        qml.CNOT(wires=[wire0, wire5])
        qml.RZ(2 * G * gamma, wires=wire5)
        qml.CNOT(wires=[wire0, wire5])
        
        qml.Barrier()

        qml.RZ(2 * q * gamma, wires=wire0)
        qml.RZ(2 * q * gamma, wires=wire1)
        qml.RZ(2 * q * gamma, wires=wire2)
        qml.RZ(2 * q * gamma, wires=wire3)
        qml.RZ(2 * q * gamma, wires=wire4)
        qml.RZ(2 * q * gamma, wires=wire5)
        qml.Barrier()
def bitstring_to_int(bit_string_sample):
       bit_string = "".join(str(bs) for bs in bit_string_sample)
       return int(bit_string, base=2)
dev = qml.device("lightning.qubit", wires=n_wires, shots=1)
wire0 = [0]
wire1 = [1]
@qml.qnode(dev)
def circuit(gammas, betas,  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])

    # during the optimization phase we are evaluating a term
    # in the objective using expval
    H = qml.PauliZ(wire0) @ qml.PauliZ(wire1) + qml.PauliZ(wire0)
    
    return qml.expval(H) 
def qaoa_1(n_layers=1):
    print("\np={:d}".format(n_layers))

    # initialize the parameters near zero
    init_params = 0.01 * np.random.rand(2, n_layers)

    # minimize the negative of the objective function
    def objective(params):
        gammas = params[0]
        betas = params[1]
        neg_obj = 0

        neg_obj -= circuit(gammas, betas, 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 {:6d}: {: .7f}".format(i + 1, -objective(params)))

    # sample measured bitstrings 100 times
    bit_strings = []
    n_samples = 100
    for i in range(n_samples):
        bit_strings.append(abs(int(circuit(params[0], params[1], n_layers=n_layers))))

    
    bit_strings_np = np.array(bit_strings)
    bit_strings_np = np.abs(bit_strings_np)
    counts = np.bincount(bit_strings_np)
    # print optimal parameters and most frequently sampled bitstring
    most_freq_bit_string = np.argmax(counts)
    print("Optimized (gamma, beta) vectors:\n{}".format(params[:, :n_layers]))

    print("Most frequently sampled bit string is: {:06b}".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
bitstrings1 = qaoa_1(n_layers=1)[1]
bitstrings2 = qaoa_1(n_layers=2)[1]
import matplotlib.pyplot as plt

xticks = range(0, 64)
xtick_labels = list(map(lambda x: format(x, "06b"), xticks))     
bins = np.arange(0, 65) - 0.5

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(16, 8))     
plt.subplot(1, 2, 1)
plt.title("n_layers=1")
plt.xlabel("bitstrings")
plt.ylabel("freq.")
plt.xticks(xticks, xtick_labels, rotation="vertical")
plt.hist(bitstrings1, bins=bins)
plt.subplot(1, 2, 2)
plt.title("n_layers=2")
plt.xlabel("bitstrings")
plt.ylabel("freq.")
plt.xticks(xticks, xtick_labels, rotation="vertical")
plt.hist(bitstrings2, bins=bins)
plt.tight_layout()
plt.show()

Hello @ming !

I ran your script and I coudn’t reproduce the error, it was fine. Would you mind sending a minimal working version of the code, the traceback error, and the output of qml.about()?

Thanks! :slightly_smiling_face:

From the running results, this code can be run without error message. I intercepted the part of the code that has problems. In the second line below, wire shows that it has not been accessed. I don’t know how to fix it.

def U_C(gamma):
for wire in range(n_wires):
wire0 = 0

Hello!

Which version of PennyLane are you using? If you send the output of qml.about() I could check your configurations.

And sorry, I am not understanding your problem. :sweat_smile:

It would be very nice if you could send me a minimal working version of the code, so I could reproduce the error and help you. If you don’t know how to do it, here is a video tutorial explaining how to prepare and send us your code :slightly_smiling_face: