1
$\begingroup$

I created a $|0\rangle$ state then applied $H$ gate to get $\frac{1}{\sqrt{2}}(|0\rangle+|1\rangle)$ and then I meausred my state. But I always found 1. I expected to find 0 and 1 uniformly random like the case of beam splitter.

Here is my code:

qubit = cirq.NamedQubit("myqubit")
circuit = cirq.Circuit(cirq.H(qubit))
for i in range(10):
    result2 = cirq.measure(qubit, key='myqubit')
    print(result2)
print(circuit)
# run simulation
result = cirq.Simulator().simulate(circuit)
print("result:")
print(result)
print(result2)

How can I find uniformly random result ?

$\endgroup$

2 Answers 2

0
$\begingroup$

You are not measuring your qubit. Look at what your print(circuit) is returning

myqubit: ───H───

You need to add the measurement gate to the circuit like so:

circuit = cirq.Circuit(cirq.H(qubit), cirq.measure(qubit, key='myqubit'))

Printing this circuit returns myqubit: ───H───M───

In your case, with the loop; you can append the gates:

for i in range(10):
    circuit.append(cirq.measure(qubit, key='myqubit'))

Printing this one returns myqubit: ───H───M───M───M───M───M───M───M───M───M───M───M───

Both programs will get you the expected uniform random measurements.

$\endgroup$
5
  • $\begingroup$ Thank you very much for your answer but when I try your method, result2 is coming as None. How can I save the result2 correctly? I tried this code scripts: saver = [] # creates an equal superposition of |0> and |1> when simulated qubit = cirq.NamedQubit("myqubit") circuit = cirq.Circuit(cirq.H(qubit), cirq.measure(qubit, key='myqubit')) for i in range(10): result2= circuit.append(cirq.measure(qubit, key='myqubit')) saver=np.append(saver,result2) $\endgroup$
    – quest
    Dec 3, 2020 at 16:51
  • $\begingroup$ And the result is not uniform... $\endgroup$
    – quest
    Dec 3, 2020 at 17:11
  • $\begingroup$ circuit.append doesn't return anything, that's why it returns None. When you call circuit.append it adds that gate to the cirq.Circuit object. What do you mean the result is not uniform? When we run the circuit 1000 times we get half and half. qubit = cirq.NamedQubit("myqubit") circuit = cirq.Circuit(cirq.H(qubit),cirq.measure(qubit, key='myqubit') ) result = cirq.Simulator().run(circuit, repetitions=1000).histogram(key='myqubit') print(result) Returns a uniform distribution Counter({1: 509, 0: 491}) $\endgroup$ Dec 3, 2020 at 17:34
  • $\begingroup$ Thanks, now I was writing to you. I did it. When I run the code, I found 400 and 600 so it seems to me not uniform. the last question is that, how can I save the just output in a list, I al trying to save but I al saving all information and I can not use [ ] for taking first element $\endgroup$
    – quest
    Dec 3, 2020 at 17:45
  • 1
    $\begingroup$ Uniform does not mean 50/50 exactly. Think about flipping a coin. When you flip a coin a thousand times, you will not get a perfect balance between Head and Tails. The result is still uniform. This program is the same thing. $\endgroup$ Dec 3, 2020 at 18:35
1
$\begingroup$

@Victory Omole already answered the question.

But here is the fixed code if you are interested.

qubit = cirq.NamedQubit("myqubit")
circuit = cirq.Circuit()
circuit = cirq.Circuit(cirq.H(qubit))
circuit.append(cirq.measure(qubit, key='result'))
print(circuit)
s=cirq.Simulator()
samples=s.run(circuit, repetitions=1000)
print('Single measurement result:' ,samples.histogram(key='result'))

print('****************************************')
circuit2 = cirq.Circuit(cirq.H(qubit))
for i in range(10):
    circuit2.append(cirq.measure(qubit, key='myqubit'))
print(circuit2)
samples2 = s.run(circuit, repetitions=1000)
print('Hadamard follows by 10 measurements result:' ,samples2.histogram(key='result'))

The output would be something like:

myqubit: ───H───M('result')───
Single measurement result: Counter({0: 501, 1: 499})
****************************************
myqubit: ───H───M───M───M───M───M───M───M───M───M───M───
Hadamard follows by 10 measurements result: Counter({1: 502, 0: 498})

Update: If you want to print out the counter, that is, accessing the result at each shot/experiment then you can do the following: (There is a better to do this probabbly but I don't really use cirq...)

samples2 = [s.run(circuit, repetitions=1) for i in range(10) ]
samples2

The output would be something like:

[result=1,
 result=0,
 result=1,
 result=1,
 result=1,
 result=0,
 result=1,
 result=0,
 result=0,
 result=0]
$\endgroup$
3
  • $\begingroup$ Thanks for the answer. How can I save the ourput in a list? I want to save just output like 01010101010 in a list however I could not access the just output part $\endgroup$
    – quest
    Dec 3, 2020 at 17:30
  • $\begingroup$ To save it to a list, append the result to a list list = [] result = cirq.Simulator().run(circuit, repetitions=1000) list.append(result) $\endgroup$ Dec 3, 2020 at 17:40
  • $\begingroup$ thanks ! I tried numpy array and it saved all information inside list but with python list, it worked Thanks :) $\endgroup$
    – quest
    Dec 3, 2020 at 17:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.