量子コンパイラを作る。 その11

# 概要
量子コンパイラを作る。
blueqatから、qiskitに変更する。
elixirで書いてみた。
orを検証。

# 成果物

https://paiza.io/projects/H-HkLmlaQdfVwjH3R5_epA


# サンプルコード

```

defmodule Asm do
	def start_link do
		Agent.start_link(fn ->
			%{in0: "", in1: "", make0: "", u: ""}
		end, name: __MODULE__)
	end
    def make0(a) do
		Agent.update(__MODULE__, fn v ->
            str = "qc = QuantumCircuit(" <> a <> ")" 
            %{v | make0: str}
		end)
	end
    def in0(a, b) do
		Agent.update(__MODULE__, fn v ->
		    if b == "1" do
                str = "qc.h(0)\nqc.h(1)"
                %{v | in0: str}
            end
		end)
    end
    def out0(a, b) do
    end
    def and0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
            str = "qc.ccx(" <> a <> ", " <> b <> ", " <> c <> ")"
            %{v | u: v.u <> str}
		end)
    end
    def xor0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
            str = "qc.cx(" <> a <> ", " <> c <> ")\nqc.cx(" <> b <> ", " <> c <> ")"
            %{v | u: v.u <> str}
		end)
    end
    def or0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
            str = "qc.ccx(" <> a <> ", " <> b <> ", " <> c <> ")\nqc.cx(" <> a <> ", " <> c <> ")\nqc.cx(" <> b <> ", " <> c <> ")"
            %{v | u: v.u <> str}
		end)
    end
    def print() do
		Agent.get(__MODULE__, fn v ->
            IO.puts "from qiskit import QuantumCircuit"
            IO.puts "from qiskit.primitives import StatevectorSampler"
            IO.puts v.make0 
            IO.puts v.in0 
            IO.puts v.u
            IO.puts "qc.measure_all()"
            IO.puts "print(qc)"
            IO.puts "sampler = StatevectorSampler()"
            IO.puts "job = sampler.run([qc], shots = 100)"
            IO.puts "result = job.result()"
            IO.puts "counts = result[0].data.meas.get_counts()"
            IO.puts "print(f\" {counts}\")"
		end)
	end
end

defmodule Main do
	def run(str) do
    	Enum.map(String.split(str, "\n"), fn l ->
		    #IO.puts l
		    s = String.split(l, " ")
		    #IO.puts Enum.at(s, 0)
		    cond do
			Enum.at(s, 0) == "make" ->
				Asm.make0(Enum.at(s, 1))
			Enum.at(s, 0) == "in" ->
				Asm.in0(Enum.at(s, 1), Enum.at(s, 2))
			Enum.at(s, 0) == "out" ->
				Asm.out0(Enum.at(s, 1), Enum.at(s, 2))
			Enum.at(s, 0) == "and" ->
				Asm.and0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			Enum.at(s, 0) == "nand" ->
				Asm.nand0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			Enum.at(s, 0) == "nor" ->
				Asm.nor0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			Enum.at(s, 0) == "xor" ->
				Asm.xor0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			Enum.at(s, 0) == "or" ->
				Asm.or0(Enum.at(s, 1), Enum.at(s, 2), Enum.at(s, 3))
			true ->
				IO.puts ""
			end
	    end)
	    Asm.print
	end
end

Asm.start_link

Main.run("""
make 3
in 0 1
out 2 2
or 0 1 2
""")







```

# 投入したソース

```
make 3
in 0 1
out 2 2
or 0 1 2

```

# 生成されたqiskit

```

from qiskit import QuantumCircuit
from qiskit.primitives import StatevectorSampler
qc = QuantumCircuit(3)
qc.h(0)
qc.h(1)
qc.ccx(0, 1, 2)
qc.cx(0, 2)
qc.cx(1, 2)
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}")



```

# qiskit実行結果

```
$ python3 test14.py
        ┌───┐                ░ ┌─┐
   q_0: ┤ H ├──■────■────────░─┤M├──────
        ├───┤  │    │        ░ └╥┘┌─┐
   q_1: ┤ H ├──■────┼────■───░──╫─┤M├───
        └───┘┌─┴─┐┌─┴─┐┌─┴─┐ ░  ║ └╥┘┌─┐
   q_2: ─────┤ X ├┤ X ├┤ X ├─░──╫──╫─┤M├
             └───┘└───┘└───┘ ░  ║  ║ └╥┘
meas: 3/════════════════════════╩══╩══╩═
                                0  1  2
 {'101': 26, '111': 29, '000': 20, '110': 25}
```

以上。