@@ -173,6 +173,28 @@ def rewrite_sx(self, node: uop.SX) -> abc.RewriteResult:
173173 cirq .XPowGate (exponent = 0.5 ).on (self .cached_qubits [0 ]), node
174174 )
175175
176+ def rewrite_ccx (self , node : uop .CCX ) -> abc .RewriteResult :
177+ # from https://algassert.com/quirk#circuit={%22cols%22:[[%22QFT3%22],[%22inputA3%22,1,1,%22+=A3%22],[1,1,1,%22%E2%80%A2%22,%22%E2%80%A2%22,%22X%22],[1,1,1,%22%E2%80%A6%22,%22%E2%80%A6%22,%22%E2%80%A6%22],[1,1,1,1,1,%22H%22],[1,1,1,1,%22%E2%80%A2%22,%22X%22],[1,1,1,1,1,%22Z^-%C2%BC%22],[1,1,1,%22%E2%80%A2%22,1,%22X%22],[1,1,1,1,1,%22Z^%C2%BC%22],[1,1,1,1,%22%E2%80%A2%22,%22X%22],[1,1,1,1,%22Z^%C2%BC%22,%22Z^-%C2%BC%22],[1,1,1,%22%E2%80%A2%22,1,%22X%22],[1,1,1,%22%E2%80%A2%22,%22X%22],[1,1,1,%22Z^%C2%BC%22,%22Z^-%C2%BC%22,%22Z^%C2%BC%22],[1,1,1,%22%E2%80%A2%22,%22X%22],[1,1,1,1,1,%22H%22]]}
178+
179+ uop .H (node .qarg ).insert_before (node )
180+ uop .CX (ctrl = node .ctrl1 , qarg = node .qarg ).insert_before (node )
181+ uop .Tdag (node .qarg ).insert_before (node )
182+ uop .CX (ctrl = node .ctrl2 , qarg = node .qarg ).insert_before (node )
183+ uop .T (node .qarg ).insert_before (node )
184+ uop .CX (ctrl = node .ctrl1 , qarg = node .qarg ).insert_before (node )
185+ uop .Tdag (node .qarg ).insert_before (node )
186+ uop .T (node .ctrl1 ).insert_before (node )
187+ uop .CX (ctrl = node .ctrl2 , qarg = node .qarg ).insert_before (node )
188+ uop .CX (ctrl = node .ctrl2 , qarg = node .ctrl1 ).insert_before (node )
189+ uop .T (node .ctrl2 ).insert_before (node )
190+ uop .Tdag (node .ctrl1 ).insert_before (node )
191+ uop .T (node .qarg ).insert_before (node )
192+ uop .CX (ctrl = node .ctrl2 , qarg = node .ctrl1 ).insert_before (node )
193+ uop .H (node .qarg ).insert_before (node )
194+ node .delete () # delete the original CCX gate
195+
196+ return abc .RewriteResult (has_done_something = True )
197+
176198 def rewrite_sxdg (self , node : uop .SXdag ) -> abc .RewriteResult :
177199 return self ._rewrite_1q_gates (
178200 cirq .XPowGate (exponent = - 0.5 ).on (self .cached_qubits [0 ]), node
@@ -394,9 +416,12 @@ def _rewrite_1q_gates(
394416 new_gate_stmts = self ._generate_1q_gate_stmts (cirq_gate , node .qarg )
395417 return self ._rewrite_gate_stmts (new_gate_stmts , node )
396418
397- def _generate_2q_ctrl_gate_stmts (
419+ def _generate_multi_ctrl_gate_stmts (
398420 self , cirq_gate : cirq .Operation , qubits_ssa : List [ir .SSAValue ]
399421 ) -> list [ir .Statement ]:
422+ qubit_to_ssa_map = {
423+ q : ssa for q , ssa in zip (self .cached_qubits [: len (qubits_ssa )], qubits_ssa )
424+ }
400425 target_gates = self .gateset .decompose_to_target_gateset (cirq_gate , 0 )
401426 new_stmts = []
402427 for new_gate in target_gates :
@@ -412,26 +437,39 @@ def _generate_2q_ctrl_gate_stmts(
412437 new_stmts .append (phi2_stmt )
413438 new_stmts .append (
414439 uop .UGate (
415- qarg = qubits_ssa [new_gate .qubits [0 ]. x ],
440+ qarg = qubit_to_ssa_map [new_gate .qubits [0 ]],
416441 theta = phi0_stmt .result ,
417442 phi = phi1_stmt .result ,
418443 lam = phi2_stmt .result ,
419444 )
420445 )
421446 else :
422447 # 2q
423- new_stmts .append (uop .CZ (ctrl = qubits_ssa [0 ], qarg = qubits_ssa [1 ]))
448+ new_stmts .append (
449+ uop .CZ (
450+ ctrl = qubit_to_ssa_map [new_gate .qubits [0 ]],
451+ qarg = qubit_to_ssa_map [new_gate .qubits [1 ]],
452+ )
453+ )
424454
425455 return new_stmts
426456
427457 def _rewrite_2q_ctrl_gates (
428458 self , cirq_gate : cirq .Operation , node : uop .TwoQubitCtrlGate
429459 ) -> abc .RewriteResult :
430- new_gate_stmts = self ._generate_2q_ctrl_gate_stmts (
460+ new_gate_stmts = self ._generate_multi_ctrl_gate_stmts (
431461 cirq_gate , [node .ctrl , node .qarg ]
432462 )
433463 return self ._rewrite_gate_stmts (new_gate_stmts , node )
434464
465+ def _rewrite_3q_ctrl_gates (
466+ self , cirq_gate : cirq .Operation , node : uop .CCX
467+ ) -> abc .RewriteResult :
468+ new_gate_stmts = self ._generate_multi_ctrl_gate_stmts (
469+ cirq_gate , [node .ctrl1 , node .ctrl2 , node .qarg ]
470+ )
471+ return self ._rewrite_gate_stmts (new_gate_stmts , node )
472+
435473 def _rewrite_gate_stmts (
436474 self , new_gate_stmts : list [ir .Statement ], node : ir .Statement
437475 ):
0 commit comments