Skip to content

Add conditional homodyne measurement#33

Merged
sansiro77 merged 17 commits intoTuringQ:mainfrom
Hugh-888:dev-kmh
Aug 29, 2024
Merged

Add conditional homodyne measurement#33
sansiro77 merged 17 commits intoTuringQ:mainfrom
Hugh-888:dev-kmh

Conversation

@Hugh-888
Copy link
Collaborator

@Hugh-888 Hugh-888 commented Aug 26, 2024

Update details

  1. Add homodyne gate circ.homodyne
    image
  2. Support homodyne measurements on arbitrary wires via arbitrary angles.
  3. Access the sampling results
    image
    if the circuits don't contain homodyne , circuit.measure_homodyne still works, but with the ideal
    multinormal distribution sampling.
  4. Support the global gaussian state collapse when performing partial homodyne measurements.
    image

@sansiro77 sansiro77 self-requested a review August 26, 2024 05:25
@sansiro77 sansiro77 self-assigned this Aug 26, 2024
@sansiro77 sansiro77 added the enhancement New feature or request label Aug 26, 2024
@sansiro77
Copy link
Contributor

sansiro77 commented Aug 26, 2024

可以参考我这个函数去简化一下forward函数。应该是不需要置换函数的,甚至连xxpp和xpxp之间的转换都不用。

from torch.distributions.multivariate_normal import MultivariateNormal

def measure_dyne(cov, mean, idx, eps):
    nmode = cov.shape[-1] // 2
    cov_m = torch.diag(torch.tensor([eps**2, 1.0 / eps**2] * (len(idx) // 2)))
    idx_rest = np.delete(np.arange(2 * nmode), idx)

    cov_a = cov[idx_rest][:, idx_rest]
    cov_b = cov[idx][:, idx]
    cov_ab = cov[idx_rest][:, idx]

    mean_a = mean[idx_rest]
    mean_b = mean[idx]
    mean_m = MultivariateNormal(mean_b, cov_b + cov_m).sample([1])[0]

    cov_out = cov_a - cov_ab @ torch.linalg.inv(cov_b + cov_m) @ cov_ab.mT
    mean_out = mean_a + cov_ab @ torch.linalg.inv(cov_b + cov_m) @ (mean_m - mean_b)

    return cov_out, mean_out, mean_m

Copy link
Contributor

@sansiro77 sansiro77 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

目前这个Homodyne没有相关的指定shots的功能。我的想法是Homodyne的forward只返回[cov, mean],用self.samples去记录采样结果。其实输入的cov如果是(batch*shots, 2n, 2n),Homodyne就已经处理了shots不为1的情况。
QumodeCircuit里的self.state只记录operators的结果,self.measurements的输出赋值给临时变量或者记录为self.state_measured之类的,并且把计算过程放到measure_homodyne里,自动判断或者加个参数来判断执行ideal还是conditional Homodyne。
Generaldyne和Homodyne继承自Operation就可以了,放到新文件measurement.py里更清楚。

@sansiro77 sansiro77 changed the title Add homodyne measurement as homodyne gate Add conditional homodyne measurement Aug 29, 2024
Copy link
Contributor

@sansiro77 sansiro77 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job!

@sansiro77 sansiro77 merged commit 88508e7 into TuringQ:main Aug 29, 2024
@Hugh-888 Hugh-888 deleted the dev-kmh branch August 29, 2024 02:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants