4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

概要

量子コンパイラを作る。
elixirで論理回路アセンブラをコンパイルして、qiskitな全加算器を生成した。

投入したソース

make 8
in 0 2
wire 3 5
xor 1 2 3
and 1 2 4
xor 0 3 7
and 0 3 5
or 4 5 6
out 6 7

生成したqiskit



from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler
qc = QuantumCircuit(8)
qc.h(0)
qc.h(1)
qc.h(2)
qc.cx(1, 3)
qc.cx(2, 3)
qc.ccx(1, 2, 4)
qc.cx(0, 7)
qc.cx(3, 7)
qc.ccx(0, 3, 5)
qc.ccx(4, 5, 6)
qc.cx(4, 6)
qc.cx(5, 6)

qc.measure_all()
print(qc)
sampler = StatevectorSampler()
job = sampler.run([qc], shots = 100)
result = job.result()
counts = result[0].data.meas.get_counts()
print(f" {counts}")
for ans, cnt in counts.items():
  print("carry = ", ans[7], " , ", ans[6], " + ", ans[5], " = ", ans[1], ", carry = ", ans[0])




成果物

qiskit実行結果

$ python3 test21.py
        ┌───┐                                              ░ ┌─┐
   q_0: ┤ H ├───────■───────────────────■──────────────────░─┤M├─────────────────────
        ├───┤       │                   │                  ░ └╥┘┌─┐
   q_1: ┤ H ├──■────┼─────────■─────────┼──────────────────░──╫─┤M├──────────────────
        ├───┤  │    │         │         │                  ░  ║ └╥┘┌─┐
   q_2: ┤ H ├──┼────┼────■────■─────────┼──────────────────░──╫──╫─┤M├───────────────
        └───┘┌─┴─┐  │  ┌─┴─┐  │         │                  ░  ║  ║ └╥┘┌─┐
   q_3: ─────┤ X ├──┼──┤ X ├──┼────■────■──────────────────░──╫──╫──╫─┤M├────────────
             └───┘  │  └───┘┌─┴─┐  │    │                  ░  ║  ║  ║ └╥┘┌─┐
   q_4: ────────────┼───────┤ X ├──┼────┼────■────■────────░──╫──╫──╫──╫─┤M├─────────
                    │       └───┘  │  ┌─┴─┐  │    │        ░  ║  ║  ║  ║ └╥┘┌─┐
   q_5: ────────────┼──────────────┼──┤ X ├──■────┼────■───░──╫──╫──╫──╫──╫─┤M├──────
                    │              │  └───┘┌─┴─┐┌─┴─┐┌─┴─┐ ░  ║  ║  ║  ║  ║ └╥┘┌─┐
   q_6: ────────────┼──────────────┼───────┤ X ├┤ X ├┤ X ├─░──╫──╫──╫──╫──╫──╫─┤M├───
                  ┌─┴─┐          ┌─┴─┐     └───┘└───┘└───┘ ░  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
   q_7: ──────────┤ X ├──────────┤ X ├─────────────────────░──╫──╫──╫──╫──╫──╫──╫─┤M├
                  └───┘          └───┘                     ░  ║  ║  ║  ║  ║  ║  ║ └╥┘
meas: 8/══════════════════════════════════════════════════════╩══╩══╩══╩══╩══╩══╩══╩═
                                                              0  1  2  3  4  5  6  7
 {'10001100': 16, '10001010': 13, '11010111': 10, '01010110': 16, '01101101': 25, '01101011': 6, '10000001': 8, '00000000': 6}
carry =  0  ,  0  +  1  =  0 , carry =  1
carry =  0  ,  1  +  0  =  0 , carry =  1
carry =  1  ,  1  +  1  =  1 , carry =  1
carry =  0  ,  1  +  1  =  1 , carry =  0
carry =  1  ,  0  +  1  =  1 , carry =  0
carry =  1  ,  1  +  0  =  1 , carry =  0
carry =  1  ,  0  +  0  =  0 , carry =  1
carry =  0  ,  0  +  0  =  0 , carry =  0

以上。

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?