@@ -336,6 +336,63 @@ impl MutVisitor for ReplaceQubitAllocation<'_> {
336
336
_ => walk_expr ( self , expr) ,
337
337
}
338
338
}
339
+
340
+ fn visit_stmt ( & mut self , stmt : & mut Stmt ) {
341
+ // This function is not called by visit_block above, so the only time it will be used is for
342
+ // top-level statement fragments. Given that, the qubits allocated will always be live for
343
+ // the entirety of a global scope, so only qubit allocations need to be generated.
344
+ match take ( & mut stmt. kind ) {
345
+ StmtKind :: Qubit ( _, pat, qubit_init, None ) => {
346
+ stmt. kind = create_qubit_global_alloc ( self . core , pat, qubit_init) ;
347
+ }
348
+ StmtKind :: Qubit ( _, pat, qubit_init, block) => {
349
+ let stmts = self . process_qubit_stmt ( stmt. span , pat, qubit_init, block) ;
350
+ * stmt = stmts[ 0 ] . clone ( ) ;
351
+ }
352
+ kind => {
353
+ stmt. kind = kind;
354
+ walk_stmt ( self , stmt) ;
355
+ }
356
+ }
357
+ }
358
+ }
359
+
360
+ fn create_qubit_global_alloc ( core : & Table , pat : Pat , qubit_init : QubitInit ) -> StmtKind {
361
+ fn qubit_alloc_expr ( core : & Table , qubit_init : QubitInit ) -> Expr {
362
+ match qubit_init. kind {
363
+ QubitInitKind :: Array ( mut expr) => creat_qubit_alloc_call_expr (
364
+ qubit_init. span ,
365
+ create_gen_core_ref ( core, "QIR.Runtime" , "AllocateQubitArray" , qubit_init. span ) ,
366
+ Some ( take ( & mut expr) ) ,
367
+ ) ,
368
+ QubitInitKind :: Single => creat_qubit_alloc_call_expr (
369
+ qubit_init. span ,
370
+ create_gen_core_ref (
371
+ core,
372
+ "QIR.Runtime" ,
373
+ "__quantum__rt__qubit_allocate" ,
374
+ qubit_init. span ,
375
+ ) ,
376
+ None ,
377
+ ) ,
378
+ QubitInitKind :: Tuple ( tup) => Expr {
379
+ id : NodeId :: default ( ) ,
380
+ span : qubit_init. span ,
381
+ ty : qubit_init. ty ,
382
+ kind : ExprKind :: Tuple (
383
+ tup. into_iter ( )
384
+ . map ( |init| qubit_alloc_expr ( core, init) )
385
+ . collect ( ) ,
386
+ ) ,
387
+ } ,
388
+ }
389
+ }
390
+
391
+ StmtKind :: Local (
392
+ Mutability :: Immutable ,
393
+ pat,
394
+ qubit_alloc_expr ( core, qubit_init) ,
395
+ )
339
396
}
340
397
341
398
fn create_general_alloc_stmt (
@@ -345,23 +402,27 @@ fn create_general_alloc_stmt(
345
402
) -> Stmt {
346
403
ident. gen_id_init (
347
404
Mutability :: Immutable ,
348
- Expr {
349
- id : NodeId :: default ( ) ,
350
- span : ident. span ,
351
- ty : Ty :: Prim ( PrimTy :: Qubit ) ,
352
- kind : ExprKind :: Call (
353
- Box :: new ( call_expr) ,
354
- Box :: new ( array_size. unwrap_or ( Expr {
355
- id : NodeId :: default ( ) ,
356
- span : ident. span ,
357
- ty : Ty :: UNIT ,
358
- kind : ExprKind :: Tuple ( vec ! [ ] ) ,
359
- } ) ) ,
360
- ) ,
361
- } ,
405
+ creat_qubit_alloc_call_expr ( ident. span , call_expr, array_size) ,
362
406
)
363
407
}
364
408
409
+ fn creat_qubit_alloc_call_expr ( span : Span , call_expr : Expr , array_size : Option < Expr > ) -> Expr {
410
+ Expr {
411
+ id : NodeId :: default ( ) ,
412
+ span,
413
+ ty : Ty :: Prim ( PrimTy :: Qubit ) ,
414
+ kind : ExprKind :: Call (
415
+ Box :: new ( call_expr) ,
416
+ Box :: new ( array_size. unwrap_or ( Expr {
417
+ id : NodeId :: default ( ) ,
418
+ span,
419
+ ty : Ty :: UNIT ,
420
+ kind : ExprKind :: Tuple ( vec ! [ ] ) ,
421
+ } ) ) ,
422
+ ) ,
423
+ }
424
+ }
425
+
365
426
fn create_general_dealloc_stmt ( call_expr : Expr , ident : & IdentTemplate ) -> Stmt {
366
427
Stmt {
367
428
id : NodeId :: default ( ) ,
0 commit comments