Trouble with "States and probabilities" Challenge

I was trying to do those challenges on the PennyLane website and for the one called “States and probabilities” I get this error:

Running submission…

        Running on public test set

              Test 1 of 2 failed! 🚫
                  Input: [1.23, 4.56]
                  Expected output: [0.2829251572359589, 0.3841937063262924, 0.1411749135148633, 0.1917062229228854]
                  Failure message: Runtime Error: Failed to execute run() function: 'ProbabilityMP' object has no attribute 'tolist'.

              Test 2 of 2 failed! 🚫
                  Input: [7.89, 0.12]
                  Expected output: [0.48026161094775754, 0.001733099740534947, 0.5161427069791757, 0.0018625823325319265]
                  Failure message: Runtime Error: Failed to execute run() function: 'ProbabilityMP' object has no attribute 'tolist'.

        Running on private test set

              One or more private tests failed. Try again! 🚫 

The code I submitted was the following:

# Create a default.qubit device with 2 qubits / wires using qml.device
dev = qml.device('default.qubit', wires = 2)
# Turn your circuit into a QNode

def circuit(angles):
    """
    The quantum circuit that you will simulate.

    Args:
        angles (list(float)): The gate angles in the circuit.

    Returns:
        (numpy.tensor): The probability vector of the underlying quantum state
        that this circuit produces.
    """

    # Put the rotation gates here
    qml.RY(angles[0], wires = 0)
    qml.RY(angles[1], wires = 1)
    return qml.probs(wires=[0, 1])

# These functions are responsible for testing the solution.
def run(test_case_input: str) -> str:
    angles = json.loads(test_case_input)
    output = circuit(angles).tolist()

    return str(output)


def check(solution_output: str, expected_output: str) -> None:
    solution_output = json.loads(solution_output)
    expected_output = json.loads(expected_output)
    assert np.allclose(solution_output, expected_output, rtol=1e-4)

I am not sure where the error is coming from. I noticed that the test calls the tolist() method on the output, but I think the problem is in my code. Does anyone know how I could fix it?

Hey @isomorfic! Welcome to the forum :rocket:

Currently, your circuit isn’t a QNode; it’s just a quantum function. The way that quantum circuits in PennyLane work is by joining two things together:

  1. Quantum function: A function that contains all of your gates and operations in the order that they appear (usually one per line).
  2. Device: The simulator backend (or real hardware!) that you want to run quantum functions on.

When you join those two things together, we call it a QNode — quantum node — but you can call it a quantum circuit for all intents and purposes :slight_smile:.

All you need to do is to decorate your quantum function circuit with @qml.qnode(dev):

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

@qml.qnode(dev)
def circuit(angles):
    ...

That should be it!

Hope this helps :slight_smile:

1 Like