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?
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!
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)
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.
Would the corrections, m_bomb = qml.measure(0, postselect=0) and qml.cond(m_det == 1, qml.Hadamard)(wires=0), be all that is needed to have the above code be submitted for completion?
yes! that’s it. I believe you are missing the postselection part in the solution you submitted in the other post, am I right? thanks!
Let me know if it worked and/or you have any further questions.
I saw your response in the other post and the adding the poselection part worked, thank you Daniela! I didn’t know how fast people respond on the forum haha
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
“”"
copy and paste your previous code here
# input four lines of code below
qml.Hadamard(wires=0)
# Measure bomb path with post-selection (0 = safe path)
m_bomb = qml.measure(0, postselect=0) # Discards shots where bomb explodes (measurement=1)
# 2nd beam-splitter: only applied to post-selected cases
qml.Hadamard(wires=0)
# Measure detector output
m_det = qml.measure(0) # detectors 1
qml.cond(m_det == 1, 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() # array of two dictionaries
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)
Please help me check and debug the code, I have tried following your tips but it is still not right. The success probability is 0.369
Hi!
recall what it is suggested in this hint PF:5.2. Problem - #2 by daniela.murcillo you are still missing a couple more steps before that last measurement m_det_2. There are two more operations after the conditional and before the second detector.