Codercise PF.1.2b - Jumbled wires

Hi, I provided the following solution but it is not accepted:

def subcircuit_1(angle, wire_list):
    """
    Implements the first subcircuit as a function of the RX gate angle
    and the list of wires wire_list on which the gates are applied
    """
    ####################
    ###YOUR CODE HERE###
    ####################
    qml.RX(angle, wires=wire_list[0])
    qml.Y(wire_list[1])

def subcircuit_2(wire_list):
    """
    Implements the second subcircuit as a function of the RX gate angle
    and the list of wires wire_list on which the gates are applied
    """
    ####################
    ###YOUR CODE HERE###
    ####################
    qml.H(wire_list[0])
    qml.CNOT(wires=[wire_list[0], wire_list[1]])

dev = qml.device("default.qubit", wires = [0,1])

@qml.qnode(dev)
def full_circuit(theta, phi):
    """
    Builds the full quantum circuit given the input parameters
    """

    ####################
    ###YOUR CODE HERE###
    ####################
    subcircuit_1(theta, wire_list=[0, 1])
    subcircuit_2(wire_list=[0, 1])
    subcircuit_1(phi, wire_list=[1, 0])

    return qml.state()

I always receive the error “Error: The full circuit QNode isn’t quite right.”

I have the same problem, I am tempting to say that It is a bug? I ran your code and print it and I got this:

print(qml.draw(full_circuit)(np.pi/3, np.pi/2))
0: ──RX(1.05)──H─╭●──Y────────┤  State
1: ──Y─────────╰X──RX(1.57)───┤  State
This is my code which it is not accepted:
def subcircuit_1(n):
    """
    Implements the first subcircuit as a function of the RX gate angle
    and the list of wires wire_list on which the gates are applied
    """
    ####################
    ###YOUR CODE HERE###
    ####################
    qml.CNOT(wires=[1, 0])

def subcircuit_1(angle, wire_list):
    """
    Implements the first subcircuit as a function of the RX gate angle
    and the list of wires wire_list on which the gates are applied
    """
    ####################
    ###YOUR CODE HERE###
    ####################
    qml.RX(angle,wires=wire_list[0])
    qml.PauliY(wires=wire_list[1])


def subcircuit_2(wire_list):
    """
    Implements the second subcircuit as a function of the RX gate angle
    and the list of wires wire_list on which the gates are applied
    """

    ####################
    ###YOUR CODE HERE###
    ####################
    qml.Hadamard(wires=wire_list[0])
    qml.CNOT(wires=wire_list)

dev = qml.device("default.qubit", wires = [0,1])

@qml.qnode(dev)
def full_circuit(theta, phi):
    """
    Builds the full quantum circuit given the input parameters
    """

    ####################
    ###YOUR CODE HERE###
    ####################
    subcircuit_1(theta,[0,1])
    subcircuit_2([0,1])
    subcircuit_1(phi,[1,0])
    
    return qml.state()

Ah thanks! I’ll wait for the fix then.

I’ve encountered the same issue you guys encountered. I’ve also noticed another error, in the template provide for this exercise: the docstring for subcircuit_2 has a copy-paste typo:

def subcircuit_2(wire_list):
    """
    Implements the second subcircuit as a function of the RX gate angle
    and the list of wires wire_list on which the gates are applied
    """

    ####################
    ###YOUR CODE HERE###
    ####################
    qml.Hadamard(wires=wire_list[0])
    qml.CNOT(wires=wire_list)

Hi @thanhnguyen14401 , @yacm and @jhanschoo ,

Thank you for pointing this out! This is indeed a bug and has now been fixed. Thank you for helping us improve the Codebook :star2:

I’ll blur your answers to avoid spoilers for others tackling this challenge in the future.

Let us know if you have any additional feedback about the Codebook! Note that the PennyLane Fundamentals module was just released this week so let us know if there’s anything else we can improve especially in this module. :heart:

1 Like

Thanks for the fix! I think there are also bugs in:
Codercise PT.2.1: I put qml.StatePrep([alpha, 0, beta, 0, gamma, 0, 0, 0], wires=range(3), normalize=True) but got wrong.
Codercise PT.2.6: I had an error with this response.

do(k)
apply(theta)
qml.adjoint(do)(k)

Codercise PF.4.2:

test_weights = np.random.rand(1, 3, 3)  # Write some valid weights here.
# test_weights = np.random.random(size=qml.StronglyEntanglingLayers.shape(n_layers=1, n_wires=3))

There’s also a typo in Circuits as functions where qml.BasicEntaglerLayers is mentioned.
Codercise PF.4.4: My answer is

dev = qml.device("default.qubit", wires = 2)

@qml.qnode(dev, diff_method = "parameter-shift", max_diff = 2)
def circuit_for_hessian(params):
    """
    Implements the circuit shown in the codercise statement
    Args:
    - params (np.ndarray): [theta_0, theta_1, theta_2, theta_3]
    Returns:
    - np.tensor: <Z0xZ1>
    """

    ####################
    ###YOUR CODE HERE###
    ####################
    qml.RY(params[0], wires=0)
    qml.IsingXX(params[1], wires=[0, 1])
    qml.RX(params[2], wires=0)
    qml.RX(params[3], wires=1)

    return qml.expval(qml.Z(0) @ qml.Z(1))  # Return the expectation value required

test_params = np.array([0.1,0.2,0.3,0.4], requires_grad = True)
# Don't change test_params! 

hessian = qml.jacobian(qml.jacobian(circuit_for_hessian))(test_params)  # Compute the Hessian
print("The hessian of the circuit is: \n", hessian)

And I got the error message: “Error: Incorrect: The output of your QNode doesn’t look quite right!.”

Since you said that there might be more bugs in this module. I’m unsure if my answers were incorrect or they are bugs.

Thanks for reporting these @thanhnguyen14401 !

We’ll check early next week and let you know here.

Have a good weekend!

Codercise PT.2.1: in this case your answer is wrong, and I initially made the same mistake as well. Think a bit more about what indexes |001\rangle, |010\rangle, and |100\rangle are.

Codercise PT.2.6: look at the definition of apply, and of IsingXX, and of the wires= argument in the template. Hence notice that apply defines a unitary on 2 wires that you want to control with a third wire. This should clue you in onto the right answer.

Codercise PF.4.2: it looks like the tester checks that test_weights is a list-of-lists. So do something like np.random.rand(1, 3, 3).tolist() instead.

Codercise PF.4.4: I have essentially the same circuit and I run into the same problem, so I think it might be a bug.

1 Like

Thanks for the hint! I got PT.2.6 and PF.4.2 figured out. And there’s a typo in PT.2.1’s docstring. I tried to do tensor product to figure out the indices of the vector, but still got wrong answer.

Oh yeah, that’s true the docstring is wrong. Anyway, alpha should go into the index for |001\rangle and not the index for |000\rangle.

Good catch @jhanschoo !! :raised_hands:

Hello, I have the same problem with the “PF.4.4 — Differentiate and repeat”. The rest is fine with me though.

I haven’t attempted the questions again, but probably a bug.

Hi all,

There was indeed a bug in PF.4.4.

It’s now in the process of getting fixed and the updated codercise should be available today or tomorrow.

Thanks again @thanhnguyen14401 , @farzanmoosavi368 , and @jhanschoo for pointing it out and helping us improve the Codebook :heart:

2 Likes

Hello, I’m stuck on PF 4.2. The following is my answer, but I’m getting an error: “list index out of range.” Could you help me identify where I went wrong?

Additionally, I’m confused about the illustration—why does the diagram show a 4-qubit circuit, while the initial state in the explanation is 3-qubit?

Thanks in advance for your help!

dev = qml.device("default.qubit", wires = 4)

@qml.qnode(dev)
def strong_entangler(params):
    """
    Applies Strongly Entangling Layers to the default initial state
    Args:
    - weights (np.ndarray): The weights argument for qml.StronglyEntanglingLayers
    Returns:
    - (np.tensor): <Z0>
    """

    ####################
    ###YOUR CODE HERE###
    ####################
    for i in range(4): 
        qml.Rot(params[i][0], params[i][1], params[i][2], wires=i)
    qml.CNOT(wires=[0, 1])
    qml.CNOT(wires=[1, 2])
    qml.CNOT(wires=[2, 3])
    qml.CNOT(wires=[3, 0])
    return qml.expval(qml.PauliZ(0))

test_weights =np.random.rand(4, 3).tolist() # Write some valid weights here.

print("The output of your circuit with these weights is: ", strong_entangler(test_weights))

I think the diagram is wrong in this case, and you should follow the code’s instructions. Additionally, you should look at qml.StronglyEntanglingLayers to obtain the correct shape.