Hi all,
So I’ve been stuck on the dynamic circuits portion in Pennylane Fundamentals. Currently, I don’t know how to proceed with the improved tester problem:
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
# 1st beam-splitter
qml.H(wires=0)
m_bomb = qml.measure(wires=0, postselect=0) # measure and postselect
# 2nd beam-splitter
qml.H(wires=0)
m_det = qml.measure(wires=0) # measure (detectors)
# input four lines of code below
qml.cond(m_det == 1, qml.H)(wires=0) # apply conditional operation
qml.measure(wires=0)
qml.H(wires=0)
m_det_2 = qml.measure(wires=0) # measure (second set of detectors)
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)
I found a post earlier from this year, PF:5.2. Problem, and have my code implemented based on the conclusions from said post. I’m not sure on the four lines of code I provided along with qml.cond, are qml.measure and qml.H correct lines to add?
Hi @eigenname
welcome to the forum.
I have answered your question in the other forum entry, but just for reference, I will answer here as well.
Your four lines are correct. You are only missing one thing regarding the measurement line that represents the bomb.
1 Like
I see, I implemented postselect=0 for the measurement before returning counts. Thank you!
1 Like
Hey there, I was wondering what’s technically wrong with this code? I figured this was, in a sense, “neater” than postselecting 0 since i thought it should ultimately return the same answer except it would be killing off the theoretical beam in the 0 state after the first half of the tester so if you wanted to you could just return qml.counts(m_det_2) for the second half and that would give oyu all the information you need. im getting an obscure error ‘00’. my code:
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
qml.Hadamard(0)
m_bomb = qml.measure(0, postselect = 0)
# 2nd beam-splitter
qml.Hadamard(0)
m_det = qml.measure(0)
# input four lines of code below
qml.cond(m_det == 1, qml.Hadamard)(wires = 0) # apply conditional operation
qml.measure(0, postselect = 1)
qml.Hadamard(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)
Hi @jwcarlson05 ! Welcome to the forum.
A live bomb placed in one of the arms acts as a measurement device. We defined a convention in which the 0 result, for this bomb measurement, indicates that the photon has gone through the free arm (those are the cases we want to look at).
I don’t understand why post selecting on the cases where the photon encounters the bomb in the second test is neater. However, I acknowledge that you can make it work operationally to get the same results, because it could be seen as a change in convention.
If you wanted to continue with your idea, you would have to start by thinking what statistics to collect and modify the return line of the function. Additionally, the success probability lines would be altered as well.
Using post selection or a conditional operator means that only the favorable cases appear in the results. I would recommend to run the code locally or modify it a bit to print the results to see clearly what I mean and also experiment and validate other ideas you come up with. This is the reason why the 00 error shows up, because there is no counts for 00 given that the post selection was changed to 1.
Let me know if this helps.
Sorry I’m still a bit confused - I figured postselecting 1 at that point in the circuit wouldn’t make a difference because the Hadamard gate will result in a 50% chance of measuring a 0 for m_det_2 regardless. And also for prob_suc_2, I am looking at 10 for m_det, m_det_2.
Hi, I think I understand your question now. Thanks for explaining.
I will get back to you as soon as possible.
Hi!
I understand what is going on now. So far, we were only focusing on the mid-circuit measurements in a sort of isolated manner. We need to think about the full program being consistent. What I mean is that in this case, it is not the same to postselect 1 or 0 at that point in the circuit and that comes from looking at the program as a whole consistent unit.
Let’s see. If we postselect on one after the conditional measurement, this means that the only way that could have succeeded was if m_d was 1. If m_d=0, then the conditional measurement is skipped and those results do not make it to our final statistics. Consequently, you would loose the successful counts from the first implementation (00 cases).
I know at first, we consider mcms as an operation performed at a particular time and this explanation might be confusing, but think about it for a while and it will make sense, specially if you consider that the program is to be consistent with physical reality.
So the easiest way to make it consistent and correct is to use postselect=0 in that mcm.
Let me know if this clarified things and thank you for asking this question. Very interesting.