PF:5.2. Problem

Hi everyone,

I’m working on the Codercise exercise that asks us to improve the success rate of the bomb tester from ~25% to ~31.25% using qml.cond(). I’m completely stuck and can’t figure out what’s going wrong.

Here’s the code I believe should be correct:

n_shots = 10000
dev = qml.device("default.qubit", shots=n_shots)
np.random.seed(0)

@qml.qnode(dev)
def circuit():
    """
    This quantum function implements an improved version of 'bomb tester'
    and returns relevant statistics with qml.counts 
    """
    qml.Hadamard(wires=0)                 # 1st beam-splitter
    m_bomb = qml.measure(0)               # measure interaction with bomb
    qml.Hadamard(wires=0)                 # 2nd beam-splitter
    m_det = qml.measure(0)                # detectors 1

    qml.cond(m_bomb == 0, qml.Hadamard)(wires=0)
    m_det_2 = qml.measure(0)

    return qml.counts(op=[m_bomb, m_det]), qml.counts(op=[m_det, m_det_2])

results = circuit()
prob_suc_1 = results[0]["00"] / n_shots
prob_suc_2 = results[1]["10"] / n_shots
prob_suc = prob_suc_1 + prob_suc_2
print("The success probability is", prob_suc)

I’ve tried changing the condition, using lambda functions, helper functions, and everything else I can think of. Still, Codercise keeps saying:
Error: Your quantum circuit isn’t quite right.

Does anyone know what the correct approach is? Or what I might be missing here?

Thanks so much for any help :folded_hands:

Hi @adri
Welcome to the forum.
Let’s take a step back and think about it.
You are in the right track. However, the hint says to think about what to do with the inconclusive cases. In what cases for m_bomb is the experiment inconclusive? Look at the diagram and apply the conditional operator for those cases.
Then, after the qml.cond, imagine that you are feeding that into another round of interaction with the bomb and so on. As though you were repeating the rest of the first cycle of operations.

Let me know if this was helpful or I can clarify further.
Thanks!

Hi Daniela,

Thanks for your previous response! I’ve been thinking more about this, and I had a couple of follow-up questions.

Wouldn’t the inconclusive cases be the ones where m_det == 1? Since in those shots we can’t really tell whether there was a bomb or not, I assume only the m_det == 0 cases (specifically “00”) count as successful interaction-free detections.

Also, I’m still a bit confused about how we are supposed to “apply the rest of the circuit again” after the qml.cond() line. As far as I understand, all subsequent gates (like the final Hadamard and second measurement) are always applied regardless of the condition. So how is the second cycle actually conditioned on m_bomb == 1?

This is the version of the script I’ve been testing. I’d really appreciate your thoughts on whether I’m applying the conditional logic correctly or missing something:

n_shots = 10000
dev = qml.device("default.qubit", shots=n_shots)
np.random.seed(0)

@qml.qnode(dev)
def circuit():
    """
    This quantum function implements an improved version of 'bomb tester'
    and returns relevant statistics with qml.counts 
    """
    qml.Hadamard(wires=0)                 # 1st beam-splitter
    m_bomb = qml.measure(0)               # measure interaction with bomb
    qml.Hadamard(wires=0)                 # 2nd beam-splitter
    m_det = qml.measure(0)                # detectors 1

    qml.cond(m_bomb == 1, qml.Hadamard)(wires=0)
    qml.measure(0)             
    qml.Hadamard(wires=0)          
    m_det_2 = qml.measure(0)
    
    return qml.counts(op=[m_bomb, m_det]), qml.counts(op=[m_det, m_det_2])

results = circuit()
prob_suc_1 = results[0]["00"] / n_shots
prob_suc_2 = results[1]["10"] / n_shots
prob_suc = prob_suc_1 + prob_suc_2
print("The success probability is", prob_suc)

Thanks again for your help and clarification!

Best,
Adrián

Hi!
Yes, you are right. According to the convention (I think there is probably a mistake in the diagram, which I’ll fix), the inconclusive cases would be for m_det==1. I want to add one thing here and it is that you should add the post-selection argument for the measurement with the bomb, I mean m_bomb = qml.measure(0, postselect=0). We are only able to account for those cases since when the photon goes the other way (1), the bomb explodes and the experiment ends. This didn’t impact the probabilities for the first codercise, but will do for this one.
Taking this into account then, the conditional operator should be conditioned on m_det==1 and not m_bomb == 1.
Your circuit is almost complete. I hope that these two fixes will lead you to the correct answer.

1 Like