Pennylane Challenges: Counting Mountains

My code passes all public tests but not the private ones. Can someone show why. Thanks.

Hello! If applicable, put your complete code example down below. Make sure that your code:

  • is 100% self-contained — someone can copy-paste exactly what is here and run it to
    reproduce the behaviour you are observing
  • includes comments
# Put code here

def U(n_wires, label):

"""
This function will define the gate U. It should not return anything,
just include the operators that define the gate.

Args:
    n_wires (int): Number of wires
    label (int): Number of mountains that we believe the function has

"""

# Put your code here #
qml.QFT(range(n_wires-1))
state=np.array(range(2**(n_wires-1)))*0
state[label]=1
state[2**(n_wires-1)- label] = 1
state = state/np.linalg.norm(state)

                      
def basis_prep(state):
    qml.MottonenStatePreparation(state_vector=state,wires=range(n_wires-1))
    return
qml.adjoint(basis_prep)(state)
def ansatz(wire):
    qml.CNOT([0,wire]) 
    return

qml.CNOT([0,n_wires-1]) 
m = qml.measure(n_wires-2)
qml.cond(m==1,ansatz)(wire=n_wires-1)   

These functions are responsible for testing the solution.

def round_if_close(x):

if round(x,3) == 0.0:
    return 0

elif round(x,3) == 1.0:
    return 1

else:
    return 0.5

def run(test_case_input: str) → str:

inputs = json.loads(test_case_input)
n_wires = int(inputs[0])
phi = np.array(inputs[1])
label = int(inputs[2])

dev = qml.device("default.qubit", wires = n_wires)
@qml.qnode(dev)
def circuit():
    qml.AmplitudeEmbedding(phi, wires = range(n_wires-1))
    U(n_wires, label)
    return qml.probs(wires = n_wires-1)

out = round_if_close(circuit()[1].numpy())

return str(out)

def check(solution_output: str, expected_output: str) → None:

solution_output = json.loads(solution_output)
expected_output = json.loads(expected_output)

assert(np.isclose(solution_output, 0.) or np.isclose(solution_output, 1.)), "Make sure that with one shot you always get the same output"

assert solution_output == expected_output, "The function did not predict the result correctly"

If you want help with diagnosing an error, please put the full error message below:

# Put full error message here

passes all public tests

          Running on private test set

                One or more private tests failed. Try again! 🚫 

And, finally, make sure to include the versions of your packages. Specifically, show us the output of qml.about().

Hi! I am not 100% sure what you are doing but I think plotting the state after the QFT may help you understand how to continue. On the other hand, you do not need measurements in this challenge :slight_smile:

Great hint. Solved the problem.