I am working on a simple program using Qiskit in python in which the programs are using evolutionarily algorithms to search for a desired circuit. I need to compare the generated circuits with desired one but I don't know how? I know that there is no truth table for quantum circuits. But for example I need to check if a circuit is working true as an adder or not. How to do this in Qiskit?


1 Answer 1


I believe the trick will be to compare the resulting state vectors. Start with your desired circuit, simulate it, and read out the state vector. Then try your auto-generated circuit, run it as well, and read out that state vector. If the parts of the state vector that represent the result match - bingo, your generated circuit is working (always advisable to test it on all possible inputs).

For example, if you want to add 3 + 5 in a $n$ qubit representation, the circuit likely does something like representing the inputs with (for example) two registers with 5 qubits each (with an additional qubit each to account for carry in the result), eg.: $$ a = |000011\rangle \\ b = |000101\rangle $$ The adder circuit must also reserve an $n+1$-register for the result (the $+1$ is again required to account for a potential carry). If you read out the result register and look for the sub-state with the highest probability, you can check whether the result has been computed correctly as: $$ {\tt result} = |001000\rangle $$

This post shows how to get the state vector from Qiskit. I'm not sure whether you are doing addition in the Fourier domain but to simulate a classic adder, I put a trivial implementation (not using Qiskit) of a 1-bit full adder here.

  • $\begingroup$ can you help me with a ternary adder circuit in qiskit to use as desired circuit? $\endgroup$
    – AMZ
    Dec 31, 2022 at 19:51
  • $\begingroup$ Something like a = b + c + d? What bit width? Using quantum gates to implement a classical full adder cascade? OR in the Fourier domain? Both would be easy for me in my code base but I'm not very proficient with Qiskit. $\endgroup$
    – rhundt
    Jan 1, 2023 at 1:10
  • $\begingroup$ See code in github.com/Qiskit/qiskit-terra/pull/5700 $\endgroup$ Jan 1, 2023 at 6:25

Your Answer

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

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