Q#のStatePreparationメモ

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

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

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

## 利用パッケージ
とりあえず以下のパッケージをOpenしといて今回のメインは```StatePreparation```です。
```C#
    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型を確率振幅に対してエンコードする方法にはこの演算を行います。

### 

### サンプルコード
```C#
    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);
    }
```

<table>
  <thead>
    <tr>
      <th style="text-align: left">Basis State<br />(|𝜓₁…𝜓ₙ⟩)</th>
      <th style="text-align: left">Amplitude</th>
      <th style="text-align: left">Measurement Probability</th>
      <th style="text-align: left" colspan="2">Phase</th>
    </tr>
  </thead>
  <tbody>
    <tr>
  <td style="text-align: left">
    <span style="display: inline-block">|00⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.7071+0.0000𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="50.00000000000002"></progress>
    <span style="display: inline-block">50.0000%</span>
  </td>
  <td style="text-align: left; transform: rotate(0.0000rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">0.0000</span>
  </td>
</tr>
<tr>
  <td style="text-align: left">
    <span style="display: inline-block">|01⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.5000+0.0000𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="25.00000000000001"></progress>
    <span style="display: inline-block">25.0000%</span>
  </td>
  <td style="text-align: left; transform: rotate(0.0000rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">0.0000</span>
  </td>
</tr>
<tr>
  <td style="text-align: left">
    <span style="display: inline-block">|10⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.3536+0.0000𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="12.500000000000004"></progress>
    <span style="display: inline-block">12.5000%</span>
  </td>
  <td style="text-align: left; transform: rotate(0.0000rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">0.0000</span>
  </td>
</tr>
<tr>
  <td style="text-align: left">
    <span style="display: inline-block">|11⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.3536+0.0000𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="12.500000000000004"></progress>
    <span style="display: inline-block">12.5000%</span>
  </td>
  <td style="text-align: left; transform: rotate(0.0000rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">0.0000</span>
  </td>
</tr>

  </tbody>
</table>


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

#### サンプルコード

```C#
    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:

<table>
  <thead>
    <tr>
      <th style="text-align: left">Basis State<br />(|𝜓₁…𝜓ₙ⟩)</th>
      <th style="text-align: left">Amplitude</th>
      <th style="text-align: left">Measurement Probability</th>
      <th style="text-align: left" colspan="2">Phase</th>
    </tr>
  </thead>
  <tbody>
    <tr>
  <td style="text-align: left">
    <span style="display: inline-block">|00⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">1.0000+0.0000𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="100"></progress>
    <span style="display: inline-block">100.0000%</span>
  </td>
  <td style="text-align: left; transform: rotate(0.0000rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">0.0000</span>
  </td>
</tr>

  </tbody>
</table>

### tolerance=0.5:

<table>
  <thead>
    <tr>
      <th style="text-align: left">Basis State<br />(|𝜓₁…𝜓ₙ⟩)</th>
      <th style="text-align: left">Amplitude</th>
      <th style="text-align: left">Measurement Probability</th>
      <th style="text-align: left" colspan="2">Phase</th>
    </tr>
  </thead>
  <tbody>
    <tr>
  <td style="text-align: left">
    <span style="display: inline-block">|00⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.4682+0.4682𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="43.841324202216136"></progress>
    <span style="display: inline-block">43.8413%</span>
  </td>
  <td style="text-align: left; transform: rotate(0.7854rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">0.7854</span>
  </td>
</tr>
<tr>
  <td style="text-align: left">
    <span style="display: inline-block">|01⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.3947+0.3947𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="31.158675797783925"></progress>
    <span style="display: inline-block">31.1587%</span>
  </td>
  <td style="text-align: left; transform: rotate(0.7854rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">0.7854</span>
  </td>
</tr>
<tr>
  <td style="text-align: left">
    <span style="display: inline-block">|10⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.2703−0.2703𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="14.613774734072038"></progress>
    <span style="display: inline-block">14.6138%</span>
  </td>
  <td style="text-align: left; transform: rotate(-0.7854rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">-0.7854</span>
  </td>
</tr>
<tr>
  <td style="text-align: left">
    <span style="display: inline-block">|11⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.2279−0.2279𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="10.386225265927969"></progress>
    <span style="display: inline-block">10.3862%</span>
  </td>
  <td style="text-align: left; transform: rotate(-0.7854rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">-0.7854</span>
  </td>
</tr>

  </tbody>
</table>


### tolerance=0.0:

<table>
  <thead>
    <tr>
      <th style="text-align: left">Basis State<br />(|𝜓₁…𝜓ₙ⟩)</th>
      <th style="text-align: left">Amplitude</th>
      <th style="text-align: left">Measurement Probability</th>
      <th style="text-align: left" colspan="2">Phase</th>
    </tr>
  </thead>
  <tbody>
    <tr>
  <td style="text-align: left">
    <span style="display: inline-block">|00⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.6533+0.2706𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="50.000000000000036"></progress>
    <span style="display: inline-block">50.0000%</span>
  </td>
  <td style="text-align: left; transform: rotate(0.3927rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">0.3927</span>
  </td>
</tr>
<tr>
  <td style="text-align: left">
    <span style="display: inline-block">|01⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.1913+0.4619𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="25.00000000000001"></progress>
    <span style="display: inline-block">25.0000%</span>
  </td>
  <td style="text-align: left; transform: rotate(1.1781rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">1.1781</span>
  </td>
</tr>
<tr>
  <td style="text-align: left">
    <span style="display: inline-block">|10⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.3266−0.1353𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="12.500000000000005"></progress>
    <span style="display: inline-block">12.5000%</span>
  </td>
  <td style="text-align: left; transform: rotate(-0.3927rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">-0.3927</span>
  </td>
</tr>
<tr>
  <td style="text-align: left">
    <span style="display: inline-block">|11⟩</span>
  </td>
  <td style="text-align: left">
    <span style="display: inline-block">0.1353−0.3266𝑖</span>
  </td>
  <td style="text-align: left">
    <progress max="100" value="12.500000000000009"></progress>
    <span style="display: inline-block">12.5000%</span>
  </td>
  <td style="text-align: left; transform: rotate(-1.1781rad)">↑</td>
  <td style="text-align: left">
    <span style="display: inline-block">-1.1781</span>
  </td>
</tr>

  </tbody>
</table>


``tolerance``って一体何なんですかね～！！！
``tolerance``って一体何なんですかね～！！！
``tolerance``って一体何なんですかね～！！！