@@ -177,6 +177,63 @@ print(pattern().full_state)
177177print (cir() / pattern().full_state)
178178```
179179
180+ - Distributed simulation of quantum circuit
181+
182+ ``` python
183+ import torch
184+ # OMP_NUM_THREADS=2 torchrun --nproc_per_node=4 main.py
185+ backend = ' gloo' # for CPU
186+ # torchrun --nproc_per_node=4 main.py
187+ backend = ' nccl' # for GPU
188+ rank, world_size, local_rank = dq.setup_distributed(backend)
189+ data = torch.arange(4 , dtype = torch.float, requires_grad = True )
190+ cir = dq.DistributedQubitCircuit(4 )
191+ cir.rylayer(encode = True )
192+ cir.cnot_ring()
193+ cir.observable(0 )
194+ cir.observable(1 , ' x' )
195+ if backend == ' nccl' :
196+ data = data.to(f ' cuda: { local_rank} ' )
197+ cir.to(f ' cuda: { local_rank} ' )
198+ state = cir(data).amps
199+ result = cir.measure(with_prob = True )
200+ exp = cir.expectation().sum()
201+ exp.backward()
202+ if rank == 0 :
203+ print (state)
204+ print (result)
205+ print (exp)
206+ print (data.grad)
207+ dq.cleanup_distributed()
208+ ```
209+
210+ - Distributed simulation of photonic quantum circuit
211+
212+ ``` python
213+ # OMP_NUM_THREADS=2 torchrun --nproc_per_node=4 main.py
214+ backend = ' gloo' # for CPU
215+ # torchrun --nproc_per_node=4 main.py
216+ backend = ' nccl' # for GPU
217+ rank, world_size, local_rank = dq.setup_distributed(backend)
218+ nmode = 4
219+ cutoff = 4
220+ data = torch.arange(14 , dtype = torch.float) / 10
221+ cir = dq.DistributedQumodeCircuit(nmode, [0 ] * nmode, cutoff)
222+ for i in range (nmode):
223+ cir.s(i, encode = True )
224+ for i in range (nmode - 1 ):
225+ cir.bs([i, i + 1 ], encode = True )
226+ if backend == ' nccl' :
227+ data = data.to(f ' cuda: { local_rank} ' )
228+ cir.to(f ' cuda: { local_rank} ' )
229+ state = cir(data).amps
230+ result = cir.measure(with_prob = True )
231+ if rank == 0 :
232+ print (state)
233+ print (result)
234+ dq.cleanup_distributed()
235+ ```
236+
180237# License
181238
182239DeepQuantum is open source, released under the Apache License, Version 2.0.
0 commit comments