Search code examples
python-3.xqiskit

My qiskit code's output differ from the Lecturer: Ryan O’Donnell


My qiskit code's output differ from the Lecturer: Ryan O’Donnell

I am testing the table shown in attached image by using qiskit in python3.8.5 and qiskit version {'qiskit-terra': '0.14.2', 'qiskit-aer': '0.5.2', 'qiskit-ignis': '0.3.3', 'qiskit-ibmq-provider': '0.7.2', 'qiskit-aqua': '0.7.3', 'qiskit': '0.19.6'}

my code is :

from qiskit import QuantumCircuit, assemble
from qiskit import Aer, execute
from qiskit.tools.visualization import plot_histogram
bit = 3
bit_lst = list(range(bit))
circuit = QuantumCircuit(bit, bit)
circuit.reset(0)
circuit.reset(1)
circuit.reset(2)
circuit.x(0)
circuit.x(1)    
circuit.ccx(0,1,2)
circuit.barrier()
circuit.measure(bit_lst,bit_lst)
circuit.draw(output='mpl')
backend = Aer.get_backend('statevector_simulator')
statevector=backend.run(assemble(circuit)).result().get_statevector()
print(statevector)
backend = Aer.get_backend('qasm_simulator')
counts1=backend.run(assemble(circuit)).result().get_counts()
print(counts1)

with open('result.txt', 'a') as f:
    print(f'011 - {statevector} - {counts1}', file=f)

plot_histogram([counts1], legend=['Simulator'])

result.txt file output is: 011 - [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j] - {'111': 1024}

as per the table the out put must be 011 but the output from the code is 111 is it my code or my knowledge of qubit?

to check wether its just a bit flip i change my code:

from qiskit import QuantumCircuit, assemble
from qiskit import Aer, execute
from qiskit.tools.visualization import plot_histogram
bit = 3
bit_lst = list(range(bit))
circuit = QuantumCircuit(bit, bit)
circuit.reset(0)
circuit.reset(1)
circuit.reset(2)

def bit_000():
    pass
def bit_001():
    circuit.x(0)
def bit_010():
    circuit.x(1)
def bit_011():
    circuit.x(0)
    circuit.x(1)    
def bit_100():
    circuit.x(2)
def bit_101():
    circuit.x(0)
    circuit.x(2)
def bit_110():
    circuit.x(1)
    circuit.x(2)
def bit_111():
    circuit.x(0)
    circuit.x(1)
    circuit.x(2)

func_lst = [bit_000, bit_001, bit_010, bit_011, bit_100, bit_101, bit_110, bit_111]
for fn in func_lst:
    fn()
    circuit.ccx(0,1,2)
    circuit.barrier()
    circuit.measure(bit_lst,bit_lst)
    circuit.draw(output='mpl')
    backend = Aer.get_backend('statevector_simulator')
    statevector=backend.run(assemble(circuit)).result().get_statevector()
    print(statevector)
    backend = Aer.get_backend('qasm_simulator')
    counts1=backend.run(assemble(circuit)).result().get_counts()
    print(counts1)

    with open('result.txt', 'a') as f:
        print(f'{fn} - {statevector} - {counts1}', file=f)

    plot_histogram([counts1], legend=['Simulator'])

result file new out put is: <function bit_000 at 0x0000028334B761F0> - [1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] - {'000': 1024}

<function bit_001 at 0x000002833524E820> - [0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] - {'001': 1024}

<function bit_010 at 0x0000028349D6CAF0> - [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j] - {'111': 1024}

<function bit_011 at 0x0000028349D6CB80> - [0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j 0.+0.j] - {'100': 1024}

<function bit_100 at 0x0000028349D6CC10> - [1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] - {'000': 1024}

<function bit_101 at 0x0000028349D6CCA0> - [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j 0.+0.j 0.+0.j] - {'101': 1024}

<function bit_110 at 0x0000028349D6CD30> - [0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 1.+0.j] - {'111': 1024}

<function bit_111 at 0x0000028349D6CDC0> - [1.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j 0.+0.j] - {'000': 1024}

didnt care about a cleaner result, Sorry. and not able to add code in reply sorry.


Solution

  • Ahh, I think I see the issue. In the table from lecturer Ryan O'Donnell, the state is read from the first qubit to the last qubit. For example, the state |110> means that the qubit labelled q_0 is in state |1>, q_1 is in state |1> and q_2 is in state |0>. In Qiskit, the label convention is reversed. So, in Qiskit, |110> would actually be read as |011>, since the state is read from the last to the first qubit. So, your code and output is correct, since in Ryan O'Donnell's table, your state |011> is |110>. If this doesn't make sense, let me know.

    As to your new code, the reason why you are getting different results than expected is that as you run your for loop, instead of creating new circuits, you are consistently adding to an existing circuit. For example, when applying the first function, bit_000, you apply to ccx gate, barrier, and measure. Then calling bit_001, you add an x gate to q_0 and do the same thing. So far everything is good. Then you add an x gate to q_1 and do the same procedure, but you get the resulting state vector '111'. That is because in qiskit, it thinks there is an x gate on the first and second qubit, so when you apply the ccx gate, you get the result you get. So really, by applying all of those circuit operations, you've made one big circuit instead of 8 isolated circuits. I would initialize all the qubits to the state |0> after each time you measure, which will give you a clean slate to work off for the next circuit. With that edit, you should get the results that are written in Ryan O'Donnell's lecture. Hope this helps!