開発者HiQ / 使い方
例
言語リポジトリにあるすべてのサンプルプログラムを、全ソース付きで掲載しています。どれもチェッカーを通り、期待される結果を最初のコメントに記しています。ファイルにコピーして、softqcos lang checkで検査してください。
- 24個のプログラム
- リポジトリの公開後はApache-2.0
HiQ Assembly
すべてのゲート、測定、分岐を正確に制御したい場合向けです。
ベルペア
測定結果が常に一致する2つの量子ビット。
// Bell pair: the two measurements always agree.program bellqubits a, bbits m0, m1h acx a, bmeasure a -> m0measure b -> m1GHZ 状態
0000または1111と読み取れる4つの量子ビット。
// GHZ state on four qubits: all bits read 0000 or 1111.program ghzqubits q[4]bits m[4]h q[0]cx q[0], q[1]cx q[1], q[2]cx q[2], q[3]repeat i in 0..4 { measure q[i] -> m[i]}テレポーテーション
2回の測定とリアルタイムの補正で状態を移します。
// Teleportation: the state ry(pi / 3)|0> moves from msg to bob,// using two measurements and real-time corrections.program teleportqubits msg, alice, bobbits m0, m1, outry(pi / 3) msg// Shared Bell pair between alice and bob.h alicecx alice, bob// Bell measurement on msg and alice.cx msg, aliceh msgmeasure msg -> m0measure alice -> m1// Corrections decided at run time.if m1 { x bob}if m0 { z bob}measure bob -> out超高密度符号化
もつれたペアの量子ビット1つで、古典ビット2つを運びます。
// Superdense coding: two classical bits travel on one qubit.// Alice applies z to set b0 and x to set b1; here she sends b0 = 0, b1 = 1.program superdensequbits alice, bobbits b0, b1h alicecx alice, bob// Encode b1 = 1 with x. A z here would also set b0.x alicecx alice, bobh alicemeasure alice -> b0measure bob -> b1ドイチュ・ジョサ
定数か均衡かを、1回のクエリで判定します。
// Deutsch-Jozsa with a balanced oracle f(x) = x0 xor x1 xor x2.// A constant oracle reads 000; this one never does.program deutsch_jozsaqubits x[3], ybits m[3]x yh yrepeat i in 0..3 { h x[i]}// Oracle: y ^= f(x).repeat i in 0..3 { cx x[i], y}repeat i in 0..3 { h x[i] measure x[i] -> m[i]}ベルンシュタイン・ヴァジラニ
隠れた文字列を1回のクエリで読み取ります。
// Bernstein-Vazirani: recover the hidden string s = 101 in one query.program bernstein_vaziraniqubits x[3], ybits m[3]x yh yrepeat i in 0..3 { h x[i]}// Oracle for s = 101: y ^= x0 xor x2.cx x[0], ycx x[2], yrepeat i in 0..3 { h x[i] measure x[i] -> m[i]}グローバー探索
印の付いた状態11を、1回の反復で見つけます。
// Grover search on two qubits for the marked state 11.// One iteration finds it with certainty.program groverqubits a, bbits ma, mbroutine diffusion(p, q) { within { h p h q x p x q } do { cz p, q }}h ah b// Oracle: phase flip on 11.cz a, bcall diffusion(a, b)measure a -> mameasure b -> mb位相推定
固有状態の位相をレジスタに読み出します。
// Phase estimation of the T gate (phase 1/8) with three counting qubits.// The counting register reads 001 (read backwards: 0.001 in binary = 1/8).program phase_estimationqubits c[3], targetbits m[3]x targetrepeat i in 0..3 { h c[i]}// Controlled T^(2^k): c[0] applies T once, c[1] twice, c[2] four times.cp(pi / 4) c[0], targetcp(pi / 2) c[1], targetcp(pi) c[2], target// Inverse QFT on the counting register.swap c[0], c[2]h c[0]cp(-pi / 2) c[1], c[0]h c[1]cp(-pi / 4) c[2], c[0]cp(-pi / 2) c[2], c[1]h c[2]repeat i in 0..3 { measure c[i] -> m[i]}成功するまで繰り返し
測定結果が1になるまで、予算16回の範囲でやり直します。
// Repeat until success: try again until the ancilla reads 1, at most 16 times,// counting the attempts, then act on the count.program repeat_until_successqubits a, ancbits muint8 attemptswhile !m budget 16 { h anc measure anc -> m reset anc attempts = attempts + 1}match attempts { 1 => { x a } 2, 3 => { z a } _ => { h a }}フーリエ変換
3量子ビットの量子フーリエ変換を101に適用します。
// Quantum Fourier transform on three qubits, applied to the basis state 101.program qftqubits q[3]bits m[3]routine qft3(a, b, c) { h a cp(pi / 2) b, a cp(pi / 4) c, a h b cp(pi / 2) c, b h c swap a, c}x q[0]x q[2]call qft3(q[0], q[1], q[2])repeat i in 0..3 { measure q[i] -> m[i]}アンシラ1つによるパリティ
各チェックが、再利用される量子ビット1つを確保し、測定し、解放します。
// Parity checks with one reused ancilla: each check allocates a fresh// qubit, measures it and releases it with a reset, so the peak is the data// qubits plus one, not plus two.program parity_ancillaqubits d[3]bits syndrome[2]x d[1]alloc anccx d[0], anccx d[1], ancmeasure anc -> syndrome[0]release anc resetalloc anccx d[1], anccx d[2], ancmeasure anc -> syndrome[1]release anc resetHiQ Script
測定し、判断し、やり直す実験向けです。
フーリエ変換
定数とループを使った量子フーリエ変換。
# Quantum Fourier transform on N qubits, for any N of 2 or more.# The controlled phase between qubits i and j is pi / 2^(j - i).program qftconst N = 4const HALF = N >> 1qubit q[N]bit m[N]x q[0]for i = 0 to N - 2 h q[i] for j = i + 1 to N - 1 cp(pi / (1 << (j - i))) q[j], q[i] nextnexth q[N - 1]for i = 0 to HALF - 1 swap q[i], q[N - 1 - i]nextfor i = 0 to N - 1 measure q[i] into m[i]next繰り返し符号
符号化し、エラーを注入し、シンドロームを読み取って訂正します。
# Three-qubit bit-flip code: encode, inject one error, measure the two# parities with a reused ancilla, and correct from the syndrome.program repetition_codequbit d[3]bit s[2], out[3]uint8 syndrome = 0# Encode |1> as |111>.x d[0]cx d[0], d[1]cx d[0], d[2]# The error: a flip on the middle qubit.x d[1]for i = 0 to 1 alloc anc cx d[i], anc cx d[i + 1], anc measure anc into s[i] free anc resetnextif s[0] then let syndrome = syndrome + 1endif s[1] then let syndrome = syndrome + 2endselect syndrome case 1: x d[0] case 3: x d[1] case 2: x d[2]endfor i = 0 to 2 measure d[i] into out[i]next並列準備
互いに素な量子ビット上のタスクを、順序が影響しないよう検査します。
# Two independent preparations written as tasks. The checker proves they# share no qubit and no variable, so the reported depth overlaps them.program parallel_preparationqubit a[2], b[2]bit ma[2], mb[2]parallel task bell_a h a[0] cx a[0], a[1] end task bell_b h b[0] cx b[0], b[1] endendfor i = 0 to 1 measure a[i] into ma[i] measure b[i] into mb[i]nextGHZ 状態
N量子ビットのGHZ。変えるのはNだけです。
# GHZ state on N qubits. Change N and nothing else.program ghzconst N = 5qubit q[N]bit m[N]h q[0]for i = 0 to N - 2 cx q[i], q[i + 1]nextfor i = 0 to N - 1 measure q[i] into m[i]nextベルンシュタイン・ヴァジラニ
隠れた文字列1011を、Nビット分のループで1回のクエリで読み取ります。
# Bernstein-Vazirani on N bits: the oracle adds every bit of x where the# hidden string s has a 1. Here s = 1011, read as q[0] q[1] q[2] q[3].program bernstein_vaziraniconst N = 4qubit x[N], ybit m[N]x yh yfor i = 0 to N - 1 h x[i]nextcx x[0], ycx x[2], ycx x[3], yfor i = 0 to N - 1 h x[i] measure x[i] into m[i]nextグローバー探索
3量子ビット、印の付いた状態111、2回の反復。
# Grover search on three qubits for the marked state 111.# Two iterations find it with probability about 0.95.program groverconst N = 3const ROUNDS = 2qubit q[N]bit m[N]sub diffusion(a, b, c) compute h a h b h c x a x b x c do ccz a, b, c endendfor i = 0 to N - 1 h q[i]nextfor r = 1 to ROUNDS ccz q[0], q[1], q[2] call diffusion(q[0], q[1], q[2])nextfor i = 0 to N - 1 measure q[i] into m[i]next成功するまで繰り返し
本体を実行し、測定結果が1になるまで、最大8回やり直します。
# Repeat until success in L2: run the body, then try again until the# measurement reads 1, at most eight times in total.program repeat_until_successqubit a, ancbit muint8 tries = 0repeat h anc measure anc into m reset anc let tries = tries + 1until m limit 8if m and tries <= 2 then x aendHiQ Objects
アルゴリズムライブラリや再利用可能な構成要素向けです。
オラクルオブジェクトを使ったグローバー
Oracleを実装するクラスなら何でも探索できます。
// Grover search with the oracle passed as an object. Any class that// implements Oracle can be searched for; dispatch is resolved at compile time.interface Oracle { def mark(a: qubit, b: qubit) }// Marks 11.class MarkEleven implements Oracle { public def mark(a: qubit, b: qubit) { cz a, b }}// Marks 01 (a = 0, b = 1).class MarkZeroOne implements Oracle { public def mark(a: qubit, b: qubit) { within { x a } do { cz a, b } }}class Search(oracle: Oracle) { public qubit a public qubit b public bit ma public bit mb def diffuse() { within { h a h b x a x b } do { cz a, b } } public def run() { h a h b oracle.mark(a, b) diffuse() measure a -> ma measure b -> mb }}main { MarkZeroOne target Search search(target) search.run()}レジスタQFT
フーリエ変換を、レジスタに対するクラスとして表現します。
// A quantum register class with a Fourier transform and its inverse,// sized at compile time. Applying qft then iqft returns the input.class Register(n: int) { public qubit q[n] public bit m[n] public def load_one(k: int) { x q[k] } public def qft() { for i in 0..n { h q[i] for j in i + 1..n { cp(pi / (1 << (j - i))) q[j], q[i] } } } public def iqft() { inverse { self.qft() } } public def read() { for i in 0..n { measure q[i] -> m[i] } }}main { Register r(3) r.load_one(1) r.qft() r.iqft() r.read()}繰り返し符号
ビットフリップ符号をクラスとして表し、エラーを加えるサブクラスを用意します。
// A three-qubit bit-flip code as a class hierarchy: a base code that// encodes and decodes, and a subclass that adds a deliberate error.class BitFlipCode { public qubit d[3] public bit s0 public bit s1 public bit out[3] public def encode() { cx d[0], d[1] cx d[0], d[2] } def error() { } public def correct() { qubit anc cx d[0], anc cx d[1], anc measure anc -> s0 reset anc cx d[1], anc cx d[2], anc measure anc -> s1 reset anc if s0 { if s1 { x d[1] } else { x d[0] } } else { if s1 { x d[2] } } } public def run() { x d[0] encode() error() correct() for i in 0..3 { measure d[i] -> out[i] } }}class NoisyChannel extends BitFlipCode { override def error() { x d[2] }}main { NoisyChannel channel channel.run()}HiQ Pulse
信号そのものを設計する制御エンジニア向けです。
ラビ
ガウス波形と定数波形から作る駆動パルス。
# A Rabi point: one Gaussian drive pulse on qubit 0, then a readout.# Sweep amp across programs to trace the Rabi oscillation.pulse rabidt 0.5 nsbits 1port d0 drive qubits 0port m0 measure qubits 0frame drive on d0 freq 5.1 GHzframe read on m0 freq 7.2 GHzwaveform pi_half gaussian duration 40 ns amp 0.25 sigma 10 nswaveform tone constant duration 1 us amp 0.1play pi_half on drivebarrierplay tone on readcapture read 1 us -> 0ラムゼー
自由発展の遅延をはさむ、2つのハーフパイパルス。
# Ramsey: two half-pi pulses separated by a free evolution delay,# with a virtual phase advance on the second pulse.pulse ramseydt 1 nsbits 1port d0 drive qubits 0port m0 measure qubits 0frame drive on d0 freq 5.1 GHzframe read on m0 freq 7.2 GHzwaveform half gaussian duration 32 ns amp 0.25 sigma 8 nsplay half on drivedelay 200 ns on driveshift_phase drive pi/2play half on drivebarriercapture read 2 us -> 0エコー
2つの遅延の間に置く再収束パルス。
# Hahn echo: half-pi, wait, pi, wait, half-pi. The pi pulse is a# Blackman window at twice the amplitude.pulse echodt 1 nsbits 1port d0 drive qubits 0port m0 measure qubits 0frame drive on d0 freq 5.1 GHzframe read on m0 freq 7.2 GHzwaveform half blackman duration 40 ns amp 0.2waveform full blackman duration 40 ns amp 0.4play half on drivedelay 500 ns on driveplay full on drivedelay 500 ns on driveplay half on drivebarriercapture read 2 us -> 0