0
$\begingroup$

I have created a circuit and got the output which matches the truth table but, I'm not understanding the measuring the circuit output in terms of the probabilities, power or error.

This is the circuit I tried to create. It is the uppg gate.

from qiskit import *
def uppg(inp1, inp2, inp3, inp4):
    
    qc = QuantumCircuit(4, 4)
    #conditions
    if(inp1 == '1'):
        qc.x(0)
    if(inp2 == '1'):
        qc.x(1)
    if(inp3 == '1'):
        qc.x(2)
    if(inp4 == '1'):
        qc.x(3)
       
    qc.barrier()
    
    #circuit
    
    qc.cx(3, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
    qc.ccx(3, 2, 1)
    qc.cx(1, 2)
    qc.cx(3, 2)
    
    #measure
    qc.measure(0, 3)
    qc.measure(1, 2)
    qc.measure(2, 1)
    qc.measure(3, 0)
    qc.draw()

    #backend
    backend = Aer.get_backend('qasm_simulator')
    job = execute(qc, backend, memory= True)
    output = job.result().get_memory()[0]
    return qc, output

    for inp1 in ['0', '1']:
        for inp2 in ['0', '1']:
            for inp3 in ['0', '1']:
                for inp4 in ['0', '1']:
                   qc_new, output = uppg(inp1, inp2, inp3, inp4)
                   print('{} {} {} {}'.format(inp1, inp2, inp3, inp4), '=', output)        
    display(qc_new.draw())

enter image description here

The above is the output I got, but I want the output to show in terms of probability or the accuracy of circuit output. How to achieve this in Qiskit?

$\endgroup$
1
  • 1
    $\begingroup$ Your code creates a circuit for each set of inputs. That is, it creates 16 different circuits. Each one of these circuits contains "classical" gates only (NOT, CNOT, and CCNOT). So, the result will be with probability 100% $\endgroup$ Jul 12 at 12:21
0
$\begingroup$

If you use memory=True in execute(), then you store the measurement outcomes for each individual shots. job.result().get_memory()[0] mean that you access measurement outcome of the first shot.

The meaning of your output is for inp1, inp2, inp3, inp4 = 0, the measurement outcome of the first shot is 0000, for inp1, inp2, inp3 = 0, and inp4 = 1, the first shot is 1011, etc.

If you want the output show in terms of probability, or counts, you can use job.result().get_counts().

Here's the code:

from qiskit import * 

def uppg(inp1, inp2, inp3, inp4):
    qc = QuantumCircuit(4, 4)
    #conditions
    if(inp1 == '1'):
        qc.x(0)
    if(inp2 == '1'):
        qc.x(1)
    if(inp3 == '1'):
        qc.x(2)
    if(inp4 == '1'):
        qc.x(3)

    qc.barrier()

    #circuit

    qc.cx(3, 1)
    qc.cx(1, 0)
    qc.cx(0, 1)
    qc.ccx(3, 2, 1)
    qc.cx(1, 2)
    qc.cx(3, 2)

    #measure
    qc.measure(0, 3)
    qc.measure(1, 2)
    qc.measure(2, 1)
    qc.measure(3, 0)
    qc.draw()

    #backend
    backend = Aer.get_backend('qasm_simulator')
    job = execute(qc, backend, shots=1024)
    output = job.result().get_counts()
    return qc, output

for inp1 in ['0', '1']:
    for inp2 in ['0', '1']:
        for inp3 in ['0', '1']:
            for inp4 in ['0', '1']:
               qc_new, output = uppg(inp1, inp2, inp3, inp4)
               print('{} {} {} {}'.format(inp1, inp2, inp3, inp4), '=', output)        
display(qc_new.draw())

output:

output

From above output, you can see that for inp1, inp2, inp3, inp4 = 0 the measurement outcomes are 0000 for all 1024 shots, meaning the probability for 0000 is 100%.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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