Skip to content

Commit

Permalink
Fixes a bug with the compilation of controlled gates
Browse files Browse the repository at this point in the history
  • Loading branch information
gadial committed Nov 5, 2024
1 parent 5994b77 commit 1e102e5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
8 changes: 4 additions & 4 deletions qiskit_aer/backends/aer_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,17 +920,17 @@ def _assemble_op(
if ctrl_state_pos > 0:
# Add x gates for ctrl qubits which state=0
ctrl_state = int(name[ctrl_state_pos+2:len(name)])
for i in range(len(qubits)):
for i in range(len(qubits)-1):
if (ctrl_state >> i) & 1 == 0:
qubits_i = [qubits[len(qubits) - 1 - i]]
qubits_i = [qubits[i]]
aer_circ.gate("x", qubits_i, params, [], conditional_reg, aer_cond_expr,
label if label else "x")
num_of_aer_ops += 1
aer_circ.gate(gate_name, qubits, params, [], conditional_reg, aer_cond_expr,
label if label else gate_name)
for i in range(len(qubits)):
for i in range(len(qubits)-1):
if (ctrl_state >> i) & 1 == 0:
qubits_i = [qubits[len(qubits) - 1 - i]]
qubits_i = [qubits[i]]
aer_circ.gate("x", qubits_i, params, [], conditional_reg, aer_cond_expr,
label if label else "x")
num_of_aer_ops += 1
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixes a bug in the compilation of circuits with controlled gates that resulted
in adding X gates on the incorrect qubits.
28 changes: 27 additions & 1 deletion test/terra/backends/aer_simulator/test_circuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
import numpy as np
from qiskit import ClassicalRegister, QuantumCircuit, QuantumRegister, assemble
from qiskit.circuit.gate import Gate
from qiskit.circuit.library.standard_gates import HGate
from qiskit.circuit.library.standard_gates import HGate, XGate, ZGate
from qiskit.quantum_info import Statevector
from test.terra.reference import ref_algorithms

from test.terra.backends.simulator_test_case import SimulatorTestCase, supported_methods
from qiskit_aer.backends import StatevectorSimulator


@ddt
Expand Down Expand Up @@ -259,3 +261,27 @@ def _define(self):
self.fail("do not reach here")
except Exception as e:
self.assertTrue('"params" is incorrect length' in repr(e))

def test_controlled_gates(self):
"""Test gates with control qubits"""
backend = StatevectorSimulator()
num_qubits = 4
circuit = QuantumCircuit(num_qubits)
cccx = XGate().control(num_ctrl_qubits=3, label=None, ctrl_state="100")
circuit.x(2)
circuit.compose(cccx, range(num_qubits), inplace=True)
job = backend.run(circuit)
state = job.result().get_statevector()
ref_state = Statevector(circuit)
self.assertEqual(state, ref_state)

num_qubits = 3
circuit = QuantumCircuit(num_qubits)
cccz = ZGate().control(num_ctrl_qubits=2, label=None, ctrl_state="10")
circuit.x(1)
circuit.x(2)
circuit.compose(cccz, range(num_qubits), inplace=True)
job = backend.run(circuit)
state = job.result().get_statevector()
ref_state = Statevector(circuit)
self.assertEqual(state, ref_state)

0 comments on commit 1e102e5

Please sign in to comment.