Skip to content

Fix the post-processing of chain-rule sampling on specific wires for Gaussian backend (#120)#127

Merged
sansiro77 merged 2 commits intoTuringQ:mainfrom
Hugh-888:hotfix/chain-rule_sample
Dec 19, 2025
Merged

Fix the post-processing of chain-rule sampling on specific wires for Gaussian backend (#120)#127
sansiro77 merged 2 commits intoTuringQ:mainfrom
Hugh-888:hotfix/chain-rule_sample

Conversation

@Hugh-888
Copy link
Copy Markdown
Collaborator

image
  • Root Cause:
    The implementation reused the same post-processing method as in the MCMC case. However, the sample structures of the two methods are different.
image image
  • Solution:
    Adding an additional post-process method for the chain-rule case.

@Hugh-888 Hugh-888 added the bugfix Fix bugs label Dec 12, 2025
@Hugh-888 Hugh-888 linked an issue Dec 12, 2025 that may be closed by this pull request
@Hugh-888
Copy link
Copy Markdown
Collaborator Author

  • Test correct Fock probability when setting with_prob=True with chain-rule method.
nmode=3
cutoff=3
data = torch.rand(2,2)

cir = dq.QumodeCircuit(nmode, cutoff=cutoff, init_state='vac', backend='gaussian')
cir.s(wires=[0], r=1, theta=0)
cir.s(wires=[1], r=1, theta=0)
cir.bs(wires=[0,1], inputs=data[0])
cir.bs(wires=[1,2], inputs=data[1])
cir()
samples = cir.measure(wires=[0, 2], shots=30, mcmc=False, with_prob=True)
samples
image
cutoff2 = 11
cir2= dq.QumodeCircuit(nmode, cutoff=cutoff2, init_state='vac', backend='gaussian')
cir2.s(wires=[0], r=1, theta=0)
cir2.s(wires=[1], r=1, theta=0)
cir2.bs(wires=[0,1], inputs=data[0])
cir2.bs(wires=[1,2], inputs=data[1])
prob_dic = cir2(is_prob=True)

for key in samples:
    prob = 0
    for k in range(cutoff2):
        temp = torch.tensor([0]*3)
        temp[1] = k
        temp[0] = key.state[0]
        temp[2] = key.state[1]
        s = dq.FockState(temp)
        if s in prob_dic:
            prob = prob + prob_dic[s]
    print(key, 'result from partial cov:', samples[key][1], 'result from full cov:', prob)
image

@Hugh-888 Hugh-888 requested a review from sansiro77 December 16, 2025 05:34
@sansiro77 sansiro77 changed the title Fix incorrect post-processing for chain-rule sampling on specific wires (#120) Fix the post-processing of chain-rule sampling on specific wires for Gaussian backend (#120) Dec 16, 2025
Comment thread src/deepquantum/photonic/circuit.py Outdated
Comment thread src/deepquantum/photonic/circuit.py
Copy link
Copy Markdown
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.

LGTM

@sansiro77 sansiro77 merged commit fa66364 into TuringQ:main Dec 19, 2025
@Hugh-888 Hugh-888 deleted the hotfix/chain-rule_sample branch January 29, 2026 10:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bugfix Fix bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Chain-rule sampling is incompatible with measurement on specifc wires

2 participants