fix cnx to work on latest terra
"""
CNX gate. N Controlled-Not Gate.
"""
-from qiskit import QuantumCircuit, CompositeGate
+from qiskit import CompositeGate
+from qiskit import QuantumCircuit
+from qiskit.extensions.standard.cx import CnotGate
+from qiskit.extensions.standard.ccx import ToffoliGate
class CNXGate(CompositeGate):
@@ -26,34 +29,34 @@ class CNXGate(CompositeGate):
def __init__(self, ctls, ancis, tgt, circ=None):
"""Create new CNX gate."""
+ self._ctl_bits = ctls
+ self._anc_bits = ancis
+ self._tgt_bits = tgt
qubits = [v for v in ctls] + [v for v in ancis] + [tgt]
n_c = len(ctls)
n_a = len(ancis)
- super(CNXGate, self).__init__("cnx", (n_c, n_a), qubits, circ)
+ super().__init__("cnx", (n_c, n_a), qubits, circ)
if n_c == 1: # cx
- self.cx(ctls[0], tgt)
+ self._attach(CnotGate(ctls[0], tgt))
elif n_c == 2: # ccx
- self.ccx(ctls[0], ctls[1], tgt)
+ self._attach(ToffoliGate(ctls[0], ctls[1], tgt))
else:
anci_idx = 0
- self.ccx(ctls[0], ctls[1], ancis[anci_idx])
+ self._attach(ToffoliGate(ctls[0], ctls[1], ancis[anci_idx]))
for idx in range(2, len(ctls) - 1):
assert anci_idx + 1 < n_a, "length of ancillary qubits are not enough, please use a large one."
- self.ccx(ctls[idx], ancis[anci_idx], ancis[anci_idx+1])
+ self._attach(ToffoliGate(ctls[idx], ancis[anci_idx], ancis[anci_idx+1]))
anci_idx += 1
- self.ccx(ctls[len(ctls)-1], ancis[anci_idx], tgt)
+ self._attach(ToffoliGate(ctls[len(ctls)-1], ancis[anci_idx], tgt))
for idx in (range(2, len(ctls) - 1))[::-1]:
- self.ccx(ctls[idx], ancis[anci_idx-1], ancis[anci_idx])
+ self._attach(ToffoliGate(ctls[idx], ancis[anci_idx-1], ancis[anci_idx]))
anci_idx -= 1
- self.ccx(ctls[0], ctls[1], ancis[anci_idx])
+ self._attach(ToffoliGate(ctls[0], ctls[1], ancis[anci_idx]))
def reapply(self, circ):
"""Reapply this gate to corresponding qubits in circ."""
- ctl_bits = [x for x in self.arg[:self.param[0]]]
- anc_bits = [x for x in self.arg[self.param[0]:self.param[0]+self.param[1]]]
- tgt_bits = self.arg[-1]
- self._modifiers(circ.cnx(ctl_bits, anc_bits, tgt_bits))
+ self._modifiers(circ.cnx(self._ctl_bits, self._anc_bits, self._tgt_bits))
def cnx(self, control_qubits, ancillary_qubits, target_qubit):
