3
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な2ビット乗算器を生成した。

投入したソース

make 12
in 0 3
and 0 2 4
and 1 2 5
and 1 3 11
and 0 3 6
xor 5 6 10
and 5 6 7
xor 4 7 9
and 4 7 8
out 9 11

生成したqiskit


from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler
def tow(s):
	if (s == "00"):
		return "0"
	if (s == "01"):
		return "1"
	if (s == "10"):
		return "2"
	if (s == "11"):
		return "3"

def fow(s):
	if (s == "0000"):
		return "0"
	if (s == "0001"):
		return "1"
	if (s == "0010"):
		return "2"
	if (s == "0011"):
		return "3"
	if (s == "0100"):
		return "4"
	if (s == "0101"):
		return "5"
	if (s == "0110"):
		return "6"
	if (s == "0111"):
		return "7"
	if (s == "1000"):
		return "8"
	if (s == "1001"):
		return "9"
	if (s == "1010"):
		return "10"
	if (s == "1011"):
		return "11"
	if (s == "1100"):
		return "12"
	if (s == "1101"):
		return "13"
	if (s == "1110"):
		return "14"
	if (s == "1111"):
		return "15"

qc = QuantumCircuit(12)
qc.h(0)
qc.h(1)
qc.h(2)
qc.h(3)
qc.ccx(0, 2, 4)
qc.ccx(1, 2, 5)
qc.ccx(1, 3, 11)
qc.ccx(0, 3, 6)
qc.cx(5, 10)
qc.cx(6, 10)
qc.ccx(5, 6, 7)
qc.cx(4, 9)
qc.cx(7, 9)
qc.ccx(4, 7, 8)

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(tow(ans[11] + ans[10]), " * ", tow(ans[9] + ans[8]), " = ", fow(ans[3] + ans[2] + ans[1] + ans[0]))





成果物

qiskit実行結果

$ python3 test22.py
         ┌───┐                                                   ░ ┌─┐
    q_0: ┤ H ├──■────────────────────────■───────────────────────░─┤M├─────────────────────────────────
         ├───┤  │                        │                       ░ └╥┘┌─┐
    q_1: ┤ H ├──┼────■─────────■─────────┼───────────────────────░──╫─┤M├──────────────────────────────
         ├───┤  │    │         │         │                       ░  ║ └╥┘┌─┐
    q_2: ┤ H ├──■────■─────────┼─────────┼───────────────────────░──╫──╫─┤M├───────────────────────────
         ├───┤  │    │         │         │                       ░  ║  ║ └╥┘┌─┐
    q_3: ┤ H ├──┼────┼─────────■─────────■───────────────────────░──╫──╫──╫─┤M├────────────────────────
         └───┘┌─┴─┐  │         │         │                       ░  ║  ║  ║ └╥┘┌─┐
    q_4: ─────┤ X ├──┼────■────┼─────────┼───────────────────■───░──╫──╫──╫──╫─┤M├─────────────────────
              └───┘┌─┴─┐  │    │         │                   │   ░  ║  ║  ║  ║ └╥┘┌─┐
    q_5: ──────────┤ X ├──┼────┼────■────┼─────────■─────────┼───░──╫──╫──╫──╫──╫─┤M├──────────────────
                   └───┘  │    │    │  ┌─┴─┐       │         │   ░  ║  ║  ║  ║  ║ └╥┘┌─┐
    q_6: ─────────────────┼────┼────┼──┤ X ├──■────■─────────┼───░──╫──╫──╫──╫──╫──╫─┤M├───────────────
                          │    │    │  └───┘  │  ┌─┴─┐       │   ░  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
    q_7: ─────────────────┼────┼────┼─────────┼──┤ X ├──■────■───░──╫──╫──╫──╫──╫──╫──╫─┤M├────────────
                          │    │    │         │  └───┘  │  ┌─┴─┐ ░  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
    q_8: ─────────────────┼────┼────┼─────────┼─────────┼──┤ X ├─░──╫──╫──╫──╫──╫──╫──╫──╫─┤M├─────────
                        ┌─┴─┐  │    │         │       ┌─┴─┐└───┘ ░  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
    q_9: ───────────────┤ X ├──┼────┼─────────┼───────┤ X ├──────░──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├──────
                        └───┘  │  ┌─┴─┐     ┌─┴─┐     └───┘      ░  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
   q_10: ──────────────────────┼──┤ X ├─────┤ X ├────────────────░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├───
                             ┌─┴─┐└───┘     └───┘                ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘┌─┐
   q_11: ────────────────────┤ X ├───────────────────────────────░──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫──╫─┤M├
                             └───┘                               ░  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║  ║ └╥┘
meas: 12/═══════════════════════════════════════════════════════════╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩══╩═
                                                                    0  1  2  3  4  5  6  7  8  9  10 11
 {'011000110111': 7, '001000010101': 7, '000000000001': 9, '011001011101': 4, '010000100110': 12, '000000000100': 9, '000000000011': 4, '000000001100': 8, '100000001010': 7, '000000000010': 2, '000000000000': 6, '100111111111': 3, '110000101110': 6, '000000001000': 7, '110001001011': 5, '010001001001': 4}
3  *  2  =  6
2  *  2  =  4
2  *  0  =  0
2  *  3  =  6
1  *  2  =  2
0  *  2  =  0
3  *  0  =  0
0  *  3  =  0
1  *  1  =  1
1  *  0  =  0
0  *  0  =  0
3  *  3  =  9
1  *  3  =  3
0  *  1  =  0
3  *  1  =  3
2  *  1  =  2

以上。

3
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
3
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?