Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove DAGNode deprecations #7902

Merged
merged 16 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
239 changes: 5 additions & 234 deletions qiskit/dagcircuit/dagnode.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,242 +16,16 @@

import warnings

from qiskit.exceptions import QiskitError


class DAGNode:
"""Parent class for DAGOpNode, DAGInNode, and DAGOutNode."""

__slots__ = ["_type", "_op", "_qargs", "_cargs", "_wire", "_node_id"]
__slots__ = ["_node_id"]

def __init__(self, type=None, op=None, name=None, qargs=None, cargs=None, wire=None, nid=-1):
def __init__(self, nid=-1):
"""Create a node"""
if type is not None:
warnings.warn(
"The DAGNode 'type' kwarg is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGOpNode, DAGInNode, or DAGOutNode instead of DAGNode.",
DeprecationWarning,
2,
)
self._type = type
if op is not None:
warnings.warn(
"The DAGNode 'op' kwarg is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date."
"Use DAGOpNode instead of DAGNode.",
DeprecationWarning,
2,
)
self._op = op
if name is not None:
warnings.warn(
"The DAGNode 'name' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"You can use 'DAGNode.op.name' if the DAGNode is of type 'op'.",
DeprecationWarning,
2,
)
if qargs is not None:
warnings.warn(
"The DAGNode 'qargs' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"You can use DAGOpNode instead of DAGNode.",
DeprecationWarning,
2,
)
self._qargs = qargs
if cargs is not None:
warnings.warn(
"The DAGNode 'cargs' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"You can use DAGOpNode instead of DAGNode.",
DeprecationWarning,
2,
)
self._cargs = cargs
if wire is not None:
warnings.warn(
"The DAGNode 'wire' kwarg is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date."
"Use DAGInNode or DAGOutNode instead of DAGNode.",
DeprecationWarning,
2,
)
self._wire = wire
self._node_id = nid

@property
def type(self):
"""Returns the type object"""
warnings.warn(
"The DAGNode 'type' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGOpNode, DAGInNode, or DAGOutNode instead of DAGNode.",
DeprecationWarning,
2,
)
return self._type

@type.setter
def type(self, dag_type):
"""Sets the value of the type object"""
warnings.warn(
"The DAGNode 'type' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGOpNode, DAGInNode, or DAGOutNode instead of DAGNode.",
DeprecationWarning,
2,
)
if dag_type not in ["op", "in", "out"]:
raise QiskitError("The DAGNode type must be 'op', 'in', or 'out'")
self._type = dag_type

@property
def op(self):
"""Returns the Instruction object corresponding to the op for the node, else None"""
warnings.warn(
"The DAGNode 'op' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGOpNode instead of DAGNode.",
DeprecationWarning,
2,
)
if not self.type or self.type != "op":
raise QiskitError("The node %s is not an op node" % (str(self)))
return self._op

@op.setter
def op(self, data):
"""Sets the op for the node"""
warnings.warn(
"The DAGNode 'op' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGOpNode instead of DAGNode.",
DeprecationWarning,
2,
)
self._op = data

@property
def name(self):
"""Returns the Instruction name corresponding to the op for this node"""
if self.type and self.type == "op":
return self.op.name
return None

@name.setter
def name(self, name):
"""Sets the Instruction name corresponding to the op for this node"""
if self.type and self.type == "op":
self.op.name = name

@property
def condition(self):
"""Returns the condition of the node.op"""
if not self.type or self.type != "op":
raise QiskitError("The node %s is not an op node" % (str(self)))
warnings.warn(
"The DAGNode 'condition' attribute is deprecated as of 0.18.0 and "
"will be removed no earlier than 3 months after the release date. "
"You can use 'DAGNode.op.condition' if the DAGNode is of type 'op'.",
DeprecationWarning,
2,
)
return self.op.condition

@condition.setter
def condition(self, new_condition):
"""Sets the node.condition which sets the node.op.condition."""
if not self.type or self.type != "op":
raise QiskitError("The node %s is not an op node" % (str(self)))
warnings.warn(
"The DAGNode 'condition' attribute is deprecated as of 0.18.0 and "
"will be removed no earlier than 3 months after the release date. "
"You can use 'DAGNode.op.condition' if the DAGNode is of type 'op'.",
DeprecationWarning,
2,
)
self.op.condition = new_condition

@property
def qargs(self):
"""
Returns list of Qubit, else an empty list.
"""
warnings.warn(
"The DAGNode 'qargs' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGOpNode instead of DAGNode.",
DeprecationWarning,
2,
)
return self._qargs

@qargs.setter
def qargs(self, new_qargs):
"""Sets the qargs to be the given list of qargs."""
warnings.warn(
"The DAGNode 'qargs' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGOpNode instead of DAGNode.",
DeprecationWarning,
2,
)
self._qargs = new_qargs

@property
def cargs(self):
"""
Returns list of Clbit, else an empty list.
"""
warnings.warn(
"The DAGNode 'cargs' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGOpNode instead of DAGNode.",
DeprecationWarning,
2,
)
return self._cargs

@cargs.setter
def cargs(self, new_cargs):
"""Sets the cargs to be the given list of cargs."""
warnings.warn(
"The DAGNode 'cargs' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGOpNode instead of DAGNode.",
DeprecationWarning,
2,
)
self._cargs = new_cargs

@property
def wire(self):
"""Returns the Bit object, else None."""
warnings.warn(
"The DAGNode 'wire' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGInNode or DAGOutNode instead of DAGNode.",
DeprecationWarning,
2,
)
if self.type not in ["in", "out"]:
raise QiskitError("The node %s is not an input/output node" % str(self))
return self._wire

@wire.setter
def wire(self, data):
"""Sets the Bit object"""
warnings.warn(
"The DAGNode 'wire' attribute is deprecated as of 0.19.0 and "
"will be removed no earlier than 3 months after the release date. "
"Use DAGInNode or DAGOutNode instead of DAGNode.",
DeprecationWarning,
2,
)
self._wire = data

def __lt__(self, other):
return self._node_id < other._node_id

Expand Down Expand Up @@ -320,12 +94,11 @@ def semantic_eq(node1, node2, bit_indices1=None, bit_indices2=None):
class DAGOpNode(DAGNode):
"""Object to represent an Instruction at a node in the DAGCircuit."""

__slots__ = ["_type", "op", "qargs", "cargs", "sort_key"]
__slots__ = ["op", "qargs", "cargs", "sort_key"]

def __init__(self, op, qargs=None, cargs=None):
"""Create an Instruction node"""
super().__init__()
self._type = "op" # Remove when DAGNode.type is removed
self.op = op
self.qargs = qargs
self.cargs = cargs
Expand All @@ -349,12 +122,11 @@ def __repr__(self):
class DAGInNode(DAGNode):
"""Object to represent an incoming wire node in the DAGCircuit."""

__slots__ = ["_type", "wire", "sort_key"]
__slots__ = ["wire", "sort_key"]

def __init__(self, wire):
"""Create an incoming node"""
super().__init__()
self._type = "in" # Remove when DAGNode.type is removed
self.wire = wire
# TODO sort_key which is used in dagcircuit.topological_nodes
# only works as str([]) for DAGInNodes. Need to figure out why.
Expand All @@ -368,12 +140,11 @@ def __repr__(self):
class DAGOutNode(DAGNode):
"""Object to represent an outgoing wire node in the DAGCircuit."""

__slots__ = ["_type", "wire", "sort_key"]
__slots__ = ["wire", "sort_key"]

def __init__(self, wire):
"""Create an outgoing node"""
super().__init__()
self._type = "out" # Remove when DAGNode.type is removed
self.wire = wire
# TODO sort_key which is used in dagcircuit.topological_nodes
# only works as str([]) for DAGOutNodes. Need to figure out why.
Expand Down
29 changes: 0 additions & 29 deletions test/python/dagcircuit/test_dagcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -1862,34 +1862,5 @@ def test_clbit_conditional(self):
)


class TestDAGDeprecations(QiskitTestCase):
"""Test DAG deprecations"""

def test_DAGNode_deprecations(self):
"""Test DAGNode deprecations."""
from qiskit.dagcircuit import DAGNode

qr = QuantumRegister(1, "qr")
cr = ClassicalRegister(1, "cr")
with self.assertWarnsRegex(DeprecationWarning, "deprecated"):
op_node = DAGNode(type="op", op=HGate(), qargs=[qr[0]], cargs=[cr[0]])
with self.assertWarnsRegex(DeprecationWarning, "deprecated"):
in_node = DAGNode(type="in", wire=qr[0])
with self.assertWarnsRegex(DeprecationWarning, "deprecated"):
out_node = DAGNode(type="out", wire=cr[0])
with self.assertWarnsRegex(DeprecationWarning, "deprecated"):
_ = op_node.type
with self.assertWarnsRegex(DeprecationWarning, "deprecated"):
_ = op_node.op
with self.assertWarnsRegex(DeprecationWarning, "deprecated"):
_ = op_node.qargs
with self.assertWarnsRegex(DeprecationWarning, "deprecated"):
_ = op_node.cargs
with self.assertWarnsRegex(DeprecationWarning, "deprecated"):
_ = in_node.wire
with self.assertWarnsRegex(DeprecationWarning, "deprecated"):
_ = out_node.wire


if __name__ == "__main__":
unittest.main()