1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

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

Last updated at Posted at 2024-12-29

概要

量子コンパイラを作る。
elixirで、論理回路アセンブラから高位合成してblueqatを出力してみた。

コマンド リファレンス

make

回路を作る。量子ビットを指定する。
inputが2つで、outputが2つ、なら4。make 4

in

inputを作る。
0,1,2がinputならば、in 3

out

outputを作る。
3,4,5がoutputならば、out 3 5

xor

xorを作る。
0,1が入力で、2が出力ならば、xor 0 1 2

and

andを作る。
0,1が入力で、2が出力ならば、and 0 1 2

or

orを作る。
0,1が入力で、2が出力ならば、or 0 1 2

投入したソース

半加算器

make 4
in 2
out 2 3
and 0 1 3
xor 0 1 2

実行結果

from blueqat import Circuit

print (Circuit(4).h[ : 2].cx[0, 3].cx[1, 3].ccx[0, 1, 2].m[ : ].run(shots = 1000))

回路図

無題.jpg

サンプルコード


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 = "Circuit(" <> a <> ")" 
            %{v | make0: str}
		end)
	end
    def in0(a) do
		Agent.update(__MODULE__, fn v ->
            str = ".h[ : " <> a <> "]"
            %{v | in0: str}
		end)
    end
    def out0(a, b) do
    end
    def and0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
            str = ".cx[" <> a <> ", " <> c <> "].cx[" <> b <> ", " <> c <> "]"
            %{v | u: v.u <> str}
		end)
    end
    def xor0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
            str = ".ccx[" <> a <> ", " <> b <> ", " <> c <> "]"
            %{v | u: v.u <> str}
		end)
    end
    def or0(a, b, c) do
		Agent.update(__MODULE__, fn v ->
            str = ".ccx[" <> a <> ", " <> b <> ", " <> c <> "].cx[" <> a <> ", " <> c <> "].cx[" <> b <> ", " <> c <> "]"
            %{v | u: v.u <> str}
		end)
    end
    def print() do
		Agent.get(__MODULE__, fn v ->
            IO.puts "from blueqat import Circuit\n\nprint (" <> v.make0 <> v.in0 <> v.u <> ".m[ : ].run(shots = 1000))\n"
		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, 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 4
in 2
out 2 3
and 0 1 3
xor 0 1 2
""")





成果物

以上。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?