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

Q#のStatePreparationメモ

Posted at

初期化に必要そうなのでメモ

環境

Modern Q#のシミュレーター上で行っています。

pip list | grep qsharp
qsharp                    1.1.3
qsharp-jupyterlab         1.1.3
qsharp-widgets            1.1.3

利用パッケージ

とりあえず以下のパッケージをOpenしといて今回のメインはStatePreparationです。

    open Microsoft.Quantum.Canon;
    open Microsoft.Quantum.Intrinsic;
    open Microsoft.Quantum.Math as Math;
    open Microsoft.Quantum.Convert as Convert;
    open Microsoft.Quantum.Unstable.StatePreparation as StatePreparation;

ExamplePreparePureStateD

Double型を確率振幅に対してエンコードする方法にはこの演算を行います。

サンプルコード

    operation ExamplePreparePureStateD(amplitudes: Double[]): Unit {
        let numSize = Length(amplitudes);
        let numQSize = Math.Round(Math.Lg(Convert.IntAsDouble(numSize)));
        use qubits = Qubit[numQSize];
        StatePreparation.PreparePureStateD(amplitudes, qubits);
        Diag.DumpMachine();
        ResetAll(qubits);
    }

        @EntryPoint()
    operation Program() : Unit {
        let amplitudes = [Math.Sqrt(0.5), Math.Sqrt(0.25),  Math.Sqrt(0.125), Math.Sqrt(0.125)];
        ExamplePreparePureStateD(amplitudes);
    }
Basis State
(|𝜓₁…𝜓ₙ⟩)
Amplitude Measurement Probability Phase
|00⟩ 0.7071+0.0000𝑖 50.0000% 0.0000
|01⟩ 0.5000+0.0000𝑖 25.0000% 0.0000
|10⟩ 0.3536+0.0000𝑖 12.5000% 0.0000
|11⟩ 0.3536+0.0000𝑖 12.5000% 0.0000

ApproximatelyPreparePureStateCP

ExamplePreparePureStateDの中身はこいつですが、こいつを使用すると確率振幅だけじゃなくて位相にもエンコードする事ができます。もちろん、どちらか一方(確率振幅or位相)という使い方もできます。

サンプルコード

    function getQubitSize<'T>(array: 'T[]): Int {
        let numSize = Length(array);
        let numQSize = Math.Round(Math.Lg(Convert.IntAsDouble(numSize)));
        numQSize
    }

    // メモ:internalは外部パッケージへの非公開設定
    internal operation ExampleApproximatelyPreparePureStateCP(tolerance: Double, amplitudes: Double[], phase: Double[]): Unit {
        let pairs = Arrays.Zipped(amplitudes, phase);
        let coefficientsAsComplexPolar = Arrays.Mapped(a -> Math.ComplexPolar(a), pairs);

        use qubits = Qubit[getQubitSize(pairs)];
        StatePreparation.ApproximatelyPreparePureStateCP(tolerance, coefficientsAsComplexPolar, qubits);
        Diag.DumpMachine();
        ResetAll(qubits);
    }

    
    @EntryPoint()
    operation Program() : Unit {
        let amplitudes = [Math.Sqrt(0.5), Math.Sqrt(0.25),  Math.Sqrt(0.125), Math.Sqrt(0.125)];
        let phases = [Math.PI()/2.0, 3.0*Math.PI()/4.0, Math.PI()/4.0, 0.0];

        ExampleApproximatelyPreparePureStateCP(1.0, amplitudes, phases);
        ExampleApproximatelyPreparePureStateCP(0.5, amplitudes, phases);
        ExampleApproximatelyPreparePureStateCP(0.0, amplitudes, phases);
    }

    

tolerance=1.0:

Basis State
(|𝜓₁…𝜓ₙ⟩)
Amplitude Measurement Probability Phase
|00⟩ 1.0000+0.0000𝑖 100.0000% 0.0000

tolerance=0.5:

Basis State
(|𝜓₁…𝜓ₙ⟩)
Amplitude Measurement Probability Phase
|00⟩ 0.4682+0.4682𝑖 43.8413% 0.7854
|01⟩ 0.3947+0.3947𝑖 31.1587% 0.7854
|10⟩ 0.2703−0.2703𝑖 14.6138% -0.7854
|11⟩ 0.2279−0.2279𝑖 10.3862% -0.7854

tolerance=0.0:

Basis State
(|𝜓₁…𝜓ₙ⟩)
Amplitude Measurement Probability Phase
|00⟩ 0.6533+0.2706𝑖 50.0000% 0.3927
|01⟩ 0.1913+0.4619𝑖 25.0000% 1.1781
|10⟩ 0.3266−0.1353𝑖 12.5000% -0.3927
|11⟩ 0.1353−0.3266𝑖 12.5000% -1.1781

toleranceって一体何なんですかね~!!!
toleranceって一体何なんですかね~!!!
toleranceって一体何なんですかね~!!!

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