# 量子测量

## 概述

$p(m)=⟨\psi|M^\dagger_mM_m|\psi⟩$

$\frac{M_m|\psi⟩}{\sqrt{⟨\psi|M^\dagger_mM_m|\psi⟩}}$

$\Sigma_mM^\dagger_mM_m=I$

$1=\Sigma_m p(m)=\Sigma_m ⟨\psi|M^\dagger_mM_m|\psi⟩$

## 计算基测量

### 单量子比特在计算基下的测量

$I=M^\dagger_0M_0+M^\dagger_1M_1=M_0+M_1$

\begin{split}\begin{align*} p(0)&=⟨\psi|M^\dagger_0M_0|\psi⟩\\ &=⟨\psi|M_0|\psi⟩\\ &=⟨\psi|(|0⟩⟨0|)|\psi⟩\\ &=(⟨\psi|0⟩)(⟨0|\psi⟩)\\ &=[(⟨0|a^{\star}+⟨1|b^{\star})|0⟩][⟨0|(a|0⟩+b|1⟩)]\\ &=(a^{\star}⟨0|0⟩+b^{\star}⟨1|0⟩)(a⟨0|0⟩+b⟨1|0⟩)\\ &=a^{\star}a\\ &=|a|^2 \end{align*}\end{split}

\begin{split}\begin{align*} \frac{M_0|\psi⟩}{|a|}=\frac{a}{|a|}|0⟩\\ \frac{M_1|\psi⟩}{|b|}=\frac{b}{|b|}|1⟩\\ \end{align*}\end{split}

### 多量子比特在计算基下的测量——以双量子比特为例

#### 测量系统中所有比特

$I=M^\dagger_{00}M_{00}+M^\dagger_{01}M_{01}+M^\dagger_{10}M_{10}+M^\dagger_{11}M_{11}=M_{00}+M_{01}+M_{10}+M_{11}$

\begin{split}\begin{align*} p(00)&=⟨\psi|M^\dagger_{00}M_{00}|\psi⟩\\ &=⟨\psi|M_{00}|\psi⟩\\ &=⟨\psi|(|00⟩⟨00|)|\psi⟩\\ &=(⟨\psi|00⟩)(⟨00|\psi⟩)\\ &=[(⟨00|a^{\star}+⟨01|b^{\star}+⟨10|c^{\star}+⟨11|d^{\star})|00⟩][⟨00|(a|00⟩+b|01⟩+c|10⟩+d|11⟩)]\\ &=(a^{\star}⟨00|00⟩+b^{\star}⟨01|00⟩+c^{\star}⟨10|00⟩+d^{\star}⟨11|00⟩)(a⟨00|00⟩+b⟨00|01⟩+c⟨00|10⟩+b⟨00|11⟩)\\ &=a^{\star}a\\ &=|a|^2 \end{align*}\end{split}

\begin{split}\begin{align*} \frac{M_{00}|\psi⟩}{|a|}=\frac{a}{|a|}|00⟩\\ \frac{M_{01}|\psi⟩}{|b|}=\frac{b}{|b|}|01⟩\\ \frac{M_{10}|\psi⟩}{|c|}=\frac{c}{|c|}|10⟩\\ \frac{M_{11}|\psi⟩}{|d|}=\frac{d}{|d|}|11⟩\\ \end{align*}\end{split}

#### 测量系统中单个比特

$I=M^\dagger_0M_0+M^\dagger_1M_1=M_0+M_1$

\begin{split}\begin{align*} p(0)&=⟨\psi|M^\dagger_0M_0|\psi⟩\\ &=⟨\psi|M_0|\psi⟩\\ &=⟨\psi|(|0⟩⟨0|\otimes I)|\psi⟩\\ &=(⟨00|a^{\star}+⟨01|b^{\star}+⟨10|c^{\star}+⟨11|d^{\star})|(|0⟩⟨0|\otimes I)|(a|00⟩+b|01⟩+c|10⟩+d|11⟩)\\ &=(⟨00|a^{\star}+⟨01|b^{\star}+⟨10|c^{\star}+⟨11|d^{\star})|(a|00⟩+b|01⟩)\\ &=a^{\star}a+b^{\star}b\\ &=|a|^2+|b|^2 \end{align*}\end{split}

\begin{split}\begin{align*} \frac{M_0|\psi⟩}{\sqrt{|a|^2+|b|^2}}=\frac{a}{\sqrt{|a|^2+|b|^2}}|00⟩+\frac{b}{\sqrt{|a|^2+|b|^2}}|01⟩\\ \frac{M_1|\psi⟩}{\sqrt{|c|^2+|d|^2}}=\frac{c}{\sqrt{|c|^2+|d|^2}}|10⟩+\frac{d}{\sqrt{|c|^2+|d|^2}}|11⟩\\ \end{align*}\end{split}

### 计算基测量的MindSpore Quantum实现

[1]:

import numpy as np                           # 导入numpy库并简写为np
from mindquantum.core.gates import X, H      # 导入量子门H, X
from mindquantum.simulator import Simulator  # 从mindquantum.simulator中导入Simulator类
from mindquantum.core.circuit import Circuit # 导入Circuit模块，用于搭建量子线路
from mindquantum.core.gates import Measure   # 引入测量门


（1）numpy是一个功能强大的Python库，主要用于对多维数组执行计算，支持大量的维度数组与矩阵运算，此外也针对数组运算提供大量的数学函数库；

（2）mindquantum是量子-经典混合计算框架，支持多种量子神经网络的训练和推理；

（3）搭建的量子线路中所需执行的量子门需要从 mindquantum.core 模块中导入；

（4）运行量子线路所需要的量子模拟器需要从 mindquantum.simulator 模块中导入；

（5）搭建量子线路所需要的量子线路类 Circuit 需要从 mindquantum.core 模块中导入；

（6）对量子线路进行测量需要从 mindquantum 中导入 Measure 操作。

#### MindSpore Quantum实现测量系统中所有比特

\begin{split}\begin{align*} p(00)&=|a|^2=(\frac{\sqrt{2}}{{2}})^2=\frac{1}{2}\\ p(01)&=|b|^2=0^2=0\\ p(10)&=|c|^2=0^2=0\\ p(11)&=|d|^2=(\frac{\sqrt{2}}{{2}})^2=\frac{1}{2}\\ \end{align*}\end{split}

\begin{split}\begin{align*} \frac{a}{|a|}|00⟩=|00⟩\\ \frac{d}{|d|}|11⟩=|11⟩\\ \end{align*}\end{split}

[2]:

circ_all = Circuit()             # 初始化量子线路
circ_all += H.on(0)              # H门作用在第0位量子比特
circ_all += X.on(1, 0)           # X门作用在第1位量子比特且受第0位量子比特控制
circ_all += Measure('q0').on(0)  # 在0号量子比特作用一个测量，并将该测量命名为'q0'
circ_all += Measure('q1').on(1)  # 在1号量子比特作用一个测量，并将该测量命名为'q1'
circ_all.svg()                   # 绘制SVG格式的量子线路图片

[2]:

[3]:

sim = Simulator('mqvector', 2)        # 声明一个2比特的mqvector模拟器
sim.apply_circuit(circ_all).svg()     # 在模拟器上运行量子线路

[3]:


[4]:

print(sim.get_qs(True))

1¦11⟩


[5]:

sim.reset()                         #复位模拟器
sim.apply_circuit(circ_all).svg()   # 在模拟器上运行量子线路

[5]:


[6]:

print(sim.get_qs(True))

1¦00⟩


[7]:

sim.reset()
result = sim.sampling(circ_all, shots=1000)  # 对上面定义的线路采样1000次
result.svg()

[7]:


#### MindSpore Quantum实现测量系统中单个比特

\begin{split}\begin{align*} p(0)=|a|^2+|b|^2=(\frac{\sqrt{2}}{{2}})^2=\frac{1}{2}\\ p(1)=|c|^2+|d|^2=(\frac{\sqrt{2}}{{2}})^2=\frac{1}{2}\\ \end{align*}\end{split}

\begin{split}\begin{align*} \frac{a}{\sqrt{|a|^2+|b|^2}}|00⟩+\frac{b}{\sqrt{|a|^2+|b|^2}}|01⟩=|00⟩\\ \frac{c}{\sqrt{|c|^2+|d|^2}}|10⟩+\frac{d}{\sqrt{|c|^2+|d|^2}}|11⟩=|11⟩\\ \end{align*}\end{split}

[8]:

circ_partial = Circuit()             # 初始化量子线路
circ_partial += H.on(0)              # H门作用在第0位量子比特
circ_partial += X.on(1, 0)           # X门作用在第1位量子比特且受第0位量子比特控制
circ_partial += Measure('q0').on(0)  # 在0号量子比特作用一个测量，并将该测量命名为'q0'
circ_partial.svg()                   # 绘制SVG格式的量子线路图片

[8]:

[9]:

sim.reset()                            # 复位模拟器
sim.apply_circuit(circ_partial).svg()  # 在模拟器上运行量子线路

[9]:


[10]:

print(sim.get_qs(True))

1¦11⟩


[11]:

sim.reset()
result = sim.sampling(circ_partial, shots=1000)  # 对上面定义的线路采样1000次
result.svg()

[11]:


## 投影测量

$M=\Sigma_{m}mP_m$

$p(m)=⟨\psi|P_m|\psi⟩$

$\frac{P_m|\psi⟩}{\sqrt{p(m)}}$

\begin{split}\begin{align*} E(M) &=\Sigma_i \lambda_i p_i\\ &=\Sigma_i \lambda_i⟨\psi|P_i|\psi⟩\\ &=⟨\psi|(\Sigma_i\lambda_i P_i)|\psi⟩\\ &=⟨\psi|M|\psi⟩ \end{align*}\end{split}

$M_mM_{m'}=\delta_{mm'}M_m$

## Pauli测量

$\begin{split}Z= \left( \begin{array}{l} 1&0\\ 0&-1 \end{array} \right)\end{split}$

$\begin{split}Z=\left( \begin{array}{l} 1&0\\ 0&-1 \end{array} \right)=1\times|0⟩⟨0|+(-1)\times|1⟩⟨1|\end{split}$

MindSpore Quantum中为我们提供了基于给定可观测量H计算投影测量期望值的功能：

get_expectation(hamiltonian) 可以计算出模拟器当前量子态关于某个观察量的期望值：$$E=⟨\psi|H|\psi⟩$$该操作不会改变量子态

[12]:

sim = Simulator('mqvector', 2)                        # 声明一个2比特的mqvector模拟器
sim.set_qs(np.array([2**0.5 / 2, 0, 0, 2**0.5 / 2]))  # 设置模拟器状态
print(sim.get_qs())

[0.70710678+0.j 0.        +0.j 0.        +0.j 0.70710678+0.j]


[13]:

from mindquantum.core.operators import Hamiltonian    # 引入哈密顿量定义模块
from mindquantum.core.operators import QubitOperator  # 引入稀疏算子定义模块

hams = Hamiltonian(QubitOperator('Z1'))               # 构建在q1上作Pauli-Z测量的哈密顿量


\begin{split}\begin{align*} E&=⟨\psi|H|\psi⟩\\&= \left( \begin{array}{l} \frac{\sqrt{2}}{2}& 0& 0& \frac{\sqrt{2}}{2} \end{array} \right) \times (Z \otimes I) \times \left( \begin{array}{l} \frac{\sqrt{2}}{2}\\ 0\\ 0\\ \frac{\sqrt{2}}{2} \end{array} \right) \\&= \left( \begin{array}{l} \frac{\sqrt{2}}{2}& 0& 0& \frac{\sqrt{2}}{2} \end{array} \right) \times \left( \begin{array}{l} 1&0\\ 0&-1\\ \end{array} \right) \otimes \left( \begin{array}{l} 1&0\\ 0&1\\ \end{array} \right) \times \left( \begin{array}{l} \frac{\sqrt{2}}{2}\\ 0\\ 0\\ \frac{\sqrt{2}}{2} \end{array} \right) \\&= \left( \begin{array}{l} \frac{\sqrt{2}}{2}& 0& 0& \frac{\sqrt{2}}{2} \end{array} \right) \times \left( \begin{array}{l} 1&0&0&0\\ 0&1&0&0\\ 0&0&-1&0\\ 0&0&0&-1 \end{array} \right) \times \left( \begin{array}{l} \frac{\sqrt{2}}{2}\\ 0\\ 0\\ \frac{\sqrt{2}}{2} \end{array} \right) \\&= 0\\ &=1\times p(1)+(-1)\times p(-1)\\ &=1\times p(1)+(-1)\times (1-p(1))\\ &=p(1)-1+p(-1)\\ \Longrightarrow&p(1)=p(-1)=0.5 \end{align*}\end{split}

[14]:

sim.get_expectation(hams)  # 计算出模拟器当前量子态关于hams的期望值

[14]:

0j


[15]:

hams2 = Hamiltonian(QubitOperator('Z0') + QubitOperator('Z1'))   # 构建在q0,q1上作Pauli-Z测量的哈密顿量


\begin{split}\begin{align*} E&=⟨\psi|H|\psi⟩\\&= \left( \begin{array}{l} \frac{\sqrt{2}}{2}& 0& 0& \frac{\sqrt{2}}{2} \end{array} \right) \times (Z \otimes I) \times \left( \begin{array}{l} \frac{\sqrt{2}}{2}\\ 0\\ 0\\ \frac{\sqrt{2}}{2} \end{array} \right) + \left( \begin{array}{l} \frac{\sqrt{2}}{2}& 0& 0& \frac{\sqrt{2}}{2} \end{array} \right) \times (I \otimes Z) \times \left( \begin{array}{l} \frac{\sqrt{2}}{2}\\ 0\\ 0\\ \frac{\sqrt{2}}{2} \end{array} \right) \\&= \left( \begin{array}{l} \frac{\sqrt{2}}{2}& 0& 0& \frac{\sqrt{2}}{2} \end{array} \right) \times \left( \begin{array}{l} 1&0\\ 0&-1\\ \end{array} \right) \otimes \left( \begin{array}{l} 1&0\\ 0&1\\ \end{array} \right) \times \left( \begin{array}{l} \frac{\sqrt{2}}{2}\\ 0\\ 0\\ \frac{\sqrt{2}}{2} \end{array} \right) + \left( \begin{array}{l} \frac{\sqrt{2}}{2}& 0& 0& \frac{\sqrt{2}}{2} \end{array} \right) \times \left( \begin{array}{l} 1&0\\ 0&1\\ \end{array} \right) \otimes \left( \begin{array}{l} 1&0\\ 0&-1\\ \end{array} \right) \times \left( \begin{array}{l} \frac{\sqrt{2}}{2}\\ 0\\ 0\\ \frac{\sqrt{2}}{2} \end{array} \right) \\&= \left( \begin{array}{l} \frac{\sqrt{2}}{2}& 0& 0& \frac{\sqrt{2}}{2} \end{array} \right) \times \left( \begin{array}{l} 1&0&0&0\\ 0&1&0&0\\ 0&0&-1&0\\ 0&0&0&-1 \end{array} \right) \times \left( \begin{array}{l} \frac{\sqrt{2}}{2}\\ 0\\ 0\\ \frac{\sqrt{2}}{2} \end{array} \right) + \left( \begin{array}{l} \frac{\sqrt{2}}{2}& 0& 0& \frac{\sqrt{2}}{2} \end{array} \right) \times \left( \begin{array}{l} 1&0&0&0\\ 0&-1&0&0\\ 0&0&1&0\\ 0&0&0&-1 \end{array} \right) \times \left( \begin{array}{l} \frac{\sqrt{2}}{2}\\ 0\\ 0\\ \frac{\sqrt{2}}{2} \end{array} \right) \\&= 0+0 \\ &=0 \end{align*}\end{split}
[16]:

sim.set_qs(np.array([2**0.5 / 2, 0, 0, 2**0.5 / 2]))  # 设置模拟器状态
sim.get_expectation(hams2)                            # 计算出模拟器当前量子态关于hams2的期望值

[16]:

0j


[17]:

sim.get_qs()

[17]:

array([0.70710678+0.j, 0.        +0.j, 0.        +0.j, 0.70710678+0.j])


[18]:

from mindquantum.utils.show_info import InfoTable

InfoTable('mindquantum', 'scipy', 'numpy')

[18]:

Software Version
mindquantum0.9.11
scipy1.10.1
numpy1.23.5
System Info
Python3.9.16
OSLinux x86_64
Memory8.3 GB
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 1, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}