Wrong outputs in Qiskit Circuit even though the implementation seem correct

  Kiến thức lập trình

So, I am making a transformation circuit for a feistel structure.here is the code:

def apply_T1(qr, cr, qc, ancilla):

# Input: 0000 -> Output: 0110
qc.x([qr[0], qr[1], qr[2], qr[3]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[1])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[2])
qc.x([qr[0], qr[1], qr[2], qr[3]])
qc.barrier()

# Input: 0001 -> Output: 1111
qc.x([qr[1], qr[2], qr[3]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[0])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[1])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[2])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[3])
qc.x([qr[1], qr[2], qr[3]])
qc.barrier()

# Input: 0010 -> Output: 1011
qc.x([qr[0], qr[2], qr[3]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[0])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[1])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[3])
qc.x([qr[0], qr[2], qr[3]])
qc.barrier()

# Input: 0011 -> Output: 0111
qc.x([qr[2], qr[3]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[0])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[1])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[2])
qc.x([qr[2], qr[3]])
qc.barrier()

# Input: 0100 -> Output: 1110
qc.x([qr[0], qr[1], qr[3]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[3])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[2])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[1])
qc.x([qr[0], qr[1], qr[3]])
qc.barrier()

# Input: 0101 -> Output: 1000
qc.x([qr[1], qr[3]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[3])
qc.x([qr[1], qr[3]])
qc.barrier()

# Input: 0110 -> Output: 0000

# Input: 0111 -> Output: 0011
qc.x(qr[3])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[0])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[1])
qc.x(qr[3])
qc.barrier()

# Input: 1000 -> Output: 0101
qc.x([qr[1], qr[2], qr[3]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[0])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[2])
qc.x([qr[1], qr[2], qr[3]])
qc.barrier()

# Input: 1001 -> Output: 1101
qc.x([qr[1], qr[2]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[0])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[2])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[3])
qc.x([qr[1], qr[2]])
qc.barrier()

# Input: 1010 -> Output: 1100
qc.x([qr[0], qr[2]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[3])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[2])
qc.x([qr[0], qr[2]])
qc.barrier()

# Input: 1011 -> Output: 0010
qc.x([qr[2]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[1])
qc.x([qr[2]])
qc.barrier()

# Input: 1100 -> Output: 1010
qc.x([qr[1], qr[0]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[1])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[3])
qc.x([qr[1], qr[0]])
qc.barrier()

# Input: 1101 -> Output: 1001
qc.x([qr[1]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[0])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[3])
qc.x([qr[1]])
qc.barrier()

# Input: 1110 -> Output: 0100
qc.x([qr[0]])
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[2])
qc.x([qr[0]])
qc.barrier()

# Input: 1111 -> Output: 0001
qc.mcx([qr[0], qr[1], qr[2], qr[3]], ancilla[0])

return qc

# following code is to test

sim = AerSimulator()

# Test Case: Input = 0000, Expected Output = 0110

qr = QuantumRegister(4)
cr = ClassicalRegister(4)
anc = QuantumRegister(4)  
qc = QuantumCircuit(qr, anc, cr)
qc = apply_T1(qr, cr, qc, anc)
qc.measure(anc, cr)
result = sim.run(qc).result()
counts = result.get_counts(qc)
print(f"Input: 0000, Expected: 0110, Actual: {counts}")

# Test Case: Input = 0001, Expected Output = 1111

qr = QuantumRegister(4)
cr = ClassicalRegister(4)
anc = QuantumRegister(4)  
qc = QuantumCircuit(qr, anc, cr)
qc.x([qr[3]])  # To create input 0001

qc = apply_T1(qr, cr, qc, anc)
qc.measure(anc, cr)

result = sim.run(qc).result()
counts = result.get_counts(qc)
print(f"Input: 0001, Expected: 1111, Actual: {counts}")

and so on for each test case.

When I try running the code to see expected versus actual outputs, I see that I am getting incorrect results even though I hardcoded the values into the circuit.

Could someone pls look into this? I would really appreciate it!

New contributor

Anany Pravin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT