Skip to content

Commit

Permalink
fix + test for controlling circuits with nontrivial phase
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderivrii committed Dec 25, 2024
1 parent 9f2ef8d commit c5f0f0e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions qiskit/transpiler/passes/synthesis/hls_plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@
HalfAdderGate,
FullAdderGate,
MultiplierGate,
GlobalPhaseGate,
)
from qiskit.circuit.annotated_operation import (
AnnotatedOperation,
Expand Down Expand Up @@ -1727,6 +1728,15 @@ def _apply_annotations(circuit: QuantumCircuit, modifiers: list[Modifier]) -> Qu

# Apply the control modifier to each gate in the circuit.
controlled_circuit = QuantumCircuit(modifier.num_ctrl_qubits + circuit.num_qubits)
if circuit.global_phase != 0:
controlled_op = GlobalPhaseGate(circuit.global_phase).control(
num_ctrl_qubits=modifier.num_ctrl_qubits,
label=None,
ctrl_state=modifier.ctrl_state,
annotated=False,
)
controlled_qubits = list(range(0, modifier.num_ctrl_qubits))
controlled_circuit.append(controlled_op, controlled_qubits)
for inst in circuit:
inst_op = inst.operation
inst_qubits = inst.qubits
Expand Down
18 changes: 18 additions & 0 deletions test/python/transpiler/test_high_level_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1617,6 +1617,24 @@ def test_annotated_circuit(self):
qct = pass_(qc)
self.assertEqual(Statevector(qc), Statevector(qct))

def test_annotated_circuit_with_phase(self):
inner = QuantumCircuit(2)
inner.global_phase = 1
inner.h(0)
inner.cx(0, 1)
gate = inner.to_gate()

qc1 = QuantumCircuit(3)
qc1.append(gate.control(annotated=False), [0, 1, 2])
qct1 = HighLevelSynthesis(basis_gates=["cx", "u"])(qc1)

qc2 = QuantumCircuit(3)
qc2.append(gate.control(annotated=True), [0, 1, 2])
qct2 = HighLevelSynthesis(basis_gates=["cx", "u"])(qc2)

self.assertEqual(Operator(qc1), Operator(qc2))
self.assertEqual(Operator(qct1), Operator(qct2))

def test_annotated_rec(self):
"""Test synthesis with annotated custom gates and recursion."""
inner2 = QuantumCircuit(2)
Expand Down

0 comments on commit c5f0f0e

Please sign in to comment.