mirror of https://github.com/Qiskit/qiskit.git
Round of deprecation removal (#3190)
* BaseModel.as_dict * _parse_basis_gates * Layout support for tuples * bit.__getitem__ * layout from_tuplelist * eq for bit * tuple as gate arguments * import warnings * test/python/test_dagcircuit.py * test.python.test_dagcircuit.TestDagRegisters.test_dag_get_qubits * rest of test.python.test_dagcircuit * fixing test.python.circuit.test_circuit_registers * test.python.circuit.test_circuit_data.TestQuantumCircuitInstructionData.test_setting_data_is_validated * test.python.circuit.test_circuit_data * lint * release note * release notes
This commit is contained in:
parent
b7d6a90211
commit
1e0d3ae18a
|
@ -15,7 +15,6 @@
|
|||
"""
|
||||
Quantum bit and Classical bit objects.
|
||||
"""
|
||||
from warnings import warn
|
||||
from qiskit.circuit.exceptions import CircuitError
|
||||
|
||||
|
||||
|
@ -45,24 +44,10 @@ class Bit:
|
|||
"""Return the official string representing the bit."""
|
||||
return "%s(%s, %s)" % (self.__class__.__name__, self.register, self.index)
|
||||
|
||||
def __getitem__(self, item):
|
||||
warn('Accessing a bit register by bit[0] or its index by bit[1] is deprecated. '
|
||||
'Go for bit.register and bit.index.', DeprecationWarning, stacklevel=2)
|
||||
if item == 0:
|
||||
return self.register
|
||||
elif item == 1:
|
||||
return self.index
|
||||
else:
|
||||
raise IndexError
|
||||
|
||||
def __hash__(self):
|
||||
return hash((self.register, self.index))
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, Bit):
|
||||
return other.index == self.index and other.register == self.register
|
||||
if isinstance(other, tuple):
|
||||
warn('Equality check between a tuple and a Bit instances is deprecated. '
|
||||
'Convert your tuples to a Bit object.', DeprecationWarning, stacklevel=2)
|
||||
return other[1] == self.index and other[0] == self.register
|
||||
return False
|
||||
|
|
|
@ -18,7 +18,6 @@ from copy import deepcopy
|
|||
import itertools
|
||||
import sys
|
||||
import multiprocessing as mp
|
||||
from warnings import warn
|
||||
from collections import OrderedDict
|
||||
from qiskit.circuit.instruction import Instruction
|
||||
from qiskit.qasm.qasm import Qasm
|
||||
|
@ -34,17 +33,6 @@ from .bit import Bit
|
|||
from .quantumcircuitdata import QuantumCircuitData
|
||||
|
||||
|
||||
def _is_bit(obj):
|
||||
"""Determine if obj is a bit"""
|
||||
# If there is a bit type this could be replaced by isinstance.
|
||||
if isinstance(obj, tuple) and len(obj) == 2:
|
||||
if isinstance(obj[0], Register) and isinstance(obj[1], int) and obj[1] < len(obj[0]):
|
||||
warn('Referring to a bit as a tuple is being deprecated. '
|
||||
'Instead go of (qr, 0), use qr[0].', DeprecationWarning)
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class QuantumCircuit:
|
||||
"""Create a new circuit.
|
||||
|
||||
|
@ -405,12 +393,6 @@ class QuantumCircuit:
|
|||
elif isinstance(bit_representation, slice):
|
||||
# circuit.h(slice(0,2)) -> circuit.h([qr[0], qr[1]])
|
||||
ret = in_array[bit_representation]
|
||||
elif _is_bit(bit_representation):
|
||||
# circuit.h((qr, 0)) -> circuit.h([qr[0]])
|
||||
ret = [bit_representation[0][bit_representation[1]]]
|
||||
elif isinstance(bit_representation, list) and \
|
||||
all(_is_bit(bit) for bit in bit_representation):
|
||||
ret = [bit[0][bit[1]] for bit in bit_representation]
|
||||
elif isinstance(bit_representation, list) and \
|
||||
all(isinstance(bit, Bit) for bit in bit_representation):
|
||||
# circuit.h([qr[0], qr[1]]) -> circuit.h([qr[0], qr[1]])
|
||||
|
|
|
@ -13,9 +13,6 @@
|
|||
# that they have been altered from the originals.
|
||||
|
||||
"""Circuit transpile function"""
|
||||
|
||||
import warnings
|
||||
|
||||
from qiskit.transpiler import Layout, CouplingMap
|
||||
from qiskit.tools.parallel import parallel_map
|
||||
from qiskit.transpiler.transpile_config import TranspileConfig
|
||||
|
@ -282,12 +279,6 @@ def _parse_basis_gates(basis_gates, backend, circuits):
|
|||
if getattr(backend, 'configuration', None):
|
||||
basis_gates = getattr(backend.configuration(), 'basis_gates', None)
|
||||
# basis_gates could be None, or a list of basis, e.g. ['u3', 'cx']
|
||||
if isinstance(basis_gates, str):
|
||||
warnings.warn("The parameter basis_gates is now a list of strings. "
|
||||
"For example, this basis ['u1','u2','u3','cx'] should be used "
|
||||
"instead of 'u1,u2,u3,cx'. The string format will be "
|
||||
"removed after 0.9", DeprecationWarning, 2)
|
||||
basis_gates = basis_gates.split(',')
|
||||
if basis_gates is None or (isinstance(basis_gates, list) and
|
||||
all(isinstance(i, str) for i in basis_gates)):
|
||||
basis_gates = [basis_gates] * len(circuits)
|
||||
|
|
|
@ -19,8 +19,6 @@ Layout is the relation between virtual (qu)bits and physical (qu)bits.
|
|||
Virtual (qu)bits are tuples, e.g. `(QuantumRegister(3, 'qr'), 2)` or simply `qr[2]`.
|
||||
Physical (qu)bits are integers.
|
||||
"""
|
||||
from warnings import warn
|
||||
|
||||
from qiskit.circuit.quantumregister import Qubit
|
||||
from qiskit.transpiler.exceptions import LayoutError
|
||||
from qiskit.converters import isinstanceint
|
||||
|
@ -77,8 +75,6 @@ class Layout():
|
|||
2: qr[2]}
|
||||
"""
|
||||
for key, value in input_dict.items():
|
||||
key = Layout._cast_tuple_to_bit(key)
|
||||
value = Layout._cast_tuple_to_bit(value)
|
||||
virtual, physical = Layout.order_based_on_type(key, value)
|
||||
self._p2v[physical] = virtual
|
||||
if virtual is None:
|
||||
|
@ -99,16 +95,7 @@ class Layout():
|
|||
' or the other way around.' % (type(value1), type(value2)))
|
||||
return virtual, physical
|
||||
|
||||
@staticmethod
|
||||
def _cast_tuple_to_bit(value):
|
||||
if isinstance(value, tuple):
|
||||
warn('Querying layout with a tuple (i.e. layout[(qr, 0)]) is deprecated. '
|
||||
'Go for layout[qr[0]].', DeprecationWarning)
|
||||
value = value[0][value[1]]
|
||||
return value
|
||||
|
||||
def __getitem__(self, item):
|
||||
item = Layout._cast_tuple_to_bit(item)
|
||||
if item in self._p2v:
|
||||
return self._p2v[item]
|
||||
if item in self._v2p:
|
||||
|
@ -116,8 +103,6 @@ class Layout():
|
|||
raise KeyError('The item %s does not exist in the Layout' % (item,))
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
key = Layout._cast_tuple_to_bit(key)
|
||||
value = Layout._cast_tuple_to_bit(value)
|
||||
virtual, physical = Layout.order_based_on_type(key, value)
|
||||
self._set_type_checked_item(virtual, physical)
|
||||
|
||||
|
@ -295,30 +280,6 @@ class Layout():
|
|||
out[int_item] = None
|
||||
return out
|
||||
|
||||
@staticmethod
|
||||
def from_tuplelist(tuple_list):
|
||||
"""
|
||||
Populates a Layout from a list containing virtual
|
||||
qubits---(QuantumRegister, int) tuples---, or None.
|
||||
|
||||
Args:
|
||||
tuple_list (list):
|
||||
e.g.: [(qr,0), None, (qr,2), (qr,3)]
|
||||
Returns:
|
||||
Layout: the corresponding Layout object
|
||||
Raises:
|
||||
LayoutError: If the elements are not (Register, integer) or None
|
||||
"""
|
||||
warn('Creating a layout with a list of tuples (eg. [(qr,0), None, (qr,2), (qr,3)]) '
|
||||
'is deprecated. Go for [qr[0], None, qr[2], qr[3]].', DeprecationWarning)
|
||||
new_list = []
|
||||
for tuple_ in tuple_list:
|
||||
if tuple_ is None:
|
||||
new_list.append(None)
|
||||
else:
|
||||
new_list.append(tuple_[0][tuple_[1]])
|
||||
return Layout.from_qubit_list(new_list)
|
||||
|
||||
@staticmethod
|
||||
def from_qubit_list(qubit_list):
|
||||
"""
|
||||
|
|
|
@ -30,8 +30,6 @@ together by using ``bind_schema``::
|
|||
pass
|
||||
"""
|
||||
|
||||
import warnings
|
||||
|
||||
from functools import wraps
|
||||
from types import SimpleNamespace, MethodType
|
||||
|
||||
|
@ -354,12 +352,6 @@ class BaseModel(SimpleNamespace):
|
|||
|
||||
return data
|
||||
|
||||
def as_dict(self):
|
||||
"""Serialize the model into a Python dict of simple types."""
|
||||
warnings.warn('The as_dict() method is deprecated, use to_dict().',
|
||||
DeprecationWarning, stacklevel=2)
|
||||
return self.to_dict()
|
||||
|
||||
|
||||
class ObjSchema(BaseSchema):
|
||||
"""Generic object schema."""
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
---
|
||||
upgrade:
|
||||
- |
|
||||
The previously deprecated representation of qubits and classical bits as tuple, which was deprecated
|
||||
in the 0.9 release, has been removed. The use of ``Qubit`` and ``Clbit`` objects is the new way to
|
||||
represent qubits and classical bits.
|
||||
- |
|
||||
The previously deprecated representation of the basis set as single string has been removed. A list
|
||||
of strings is the nre prefered way.
|
||||
- |
|
||||
The method ``BaseModel.as_dict``, which was deprecated in the 0.9 release, has been removed in favor
|
||||
of the method ``BaseModel.to_dict``.
|
|
@ -324,7 +324,7 @@ class TestQuantumCircuitInstructionData(QiskitTestCase):
|
|||
|
||||
qc.data.append((HGate(), [qr[0]], []))
|
||||
qc.data.append((CnotGate(), [0, 1], []))
|
||||
qc.data.append((HGate(), [(qr, 1)], []))
|
||||
qc.data.append((HGate(), [qr[1]], []))
|
||||
|
||||
expected_qc = QuantumCircuit(qr)
|
||||
|
||||
|
@ -344,7 +344,7 @@ class TestQuantumCircuitInstructionData(QiskitTestCase):
|
|||
|
||||
qc.data.insert(0, (HGate(), [qr[0]], []))
|
||||
qc.data.insert(1, (CnotGate(), [0, 1], []))
|
||||
qc.data.insert(2, (HGate(), [(qr, 1)], []))
|
||||
qc.data.insert(2, (HGate(), [qr[1]], []))
|
||||
|
||||
expected_qc = QuantumCircuit(qr)
|
||||
|
||||
|
@ -364,7 +364,7 @@ class TestQuantumCircuitInstructionData(QiskitTestCase):
|
|||
|
||||
qc.data.extend([(HGate(), [qr[0]], []),
|
||||
(CnotGate(), [0, 1], []),
|
||||
(HGate(), [(qr, 1)], [])])
|
||||
(HGate(), [qr[1]], [])])
|
||||
|
||||
expected_qc = QuantumCircuit(qr)
|
||||
|
||||
|
@ -384,7 +384,7 @@ class TestQuantumCircuitInstructionData(QiskitTestCase):
|
|||
|
||||
qc.data = [(HGate(), [qr[0]], []),
|
||||
(CnotGate(), [0, 1], []),
|
||||
(HGate(), [(qr, 1)], [])]
|
||||
(HGate(), [qr[1]], [])]
|
||||
|
||||
expected_qc = QuantumCircuit(qr)
|
||||
|
||||
|
|
|
@ -419,40 +419,3 @@ class TestCircuitRegisters(QiskitTestCase):
|
|||
for (gate, qargs, _) in circ.data:
|
||||
self.assertEqual(gate.name, 'unitary')
|
||||
self.assertEqual(len(qargs), 4)
|
||||
|
||||
|
||||
class TestCircuitBit(QiskitTestCase):
|
||||
"""QuantumCircuit Registers tests."""
|
||||
|
||||
def test_bit_getitem(self):
|
||||
""" Deprecated Bit.__getitem__.
|
||||
"""
|
||||
qubit = QuantumRegister(1, "q")[0]
|
||||
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
self.assertEqual(qubit[0], qubit.register)
|
||||
self.assertEqual(qubit[1], qubit.index)
|
||||
|
||||
def test_gate_with_tuples(self):
|
||||
""" Deprecated gate parameters as tuples"""
|
||||
qr = QuantumRegister(1)
|
||||
qc = QuantumCircuit(qr)
|
||||
|
||||
expected = QuantumCircuit(qr)
|
||||
expected.h(qr[0])
|
||||
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
qc.h((qr, 0))
|
||||
self.assertEqual(qc, expected)
|
||||
|
||||
def test_gate_with_tuple_list(self):
|
||||
""" Deprecated gate parameters as tuple list"""
|
||||
qr = QuantumRegister(2)
|
||||
qc = QuantumCircuit(qr)
|
||||
|
||||
expected = QuantumCircuit(qr)
|
||||
expected.h([qr[0], qr[1]])
|
||||
|
||||
with self.assertWarns(DeprecationWarning):
|
||||
qc.h([(qr, 0), (qr, 1)])
|
||||
self.assertEqual(qc, expected)
|
||||
|
|
|
@ -21,7 +21,7 @@ from ddt import ddt, data
|
|||
import networkx as nx
|
||||
|
||||
from qiskit.dagcircuit import DAGCircuit
|
||||
from qiskit.circuit import QuantumRegister, Qubit
|
||||
from qiskit.circuit import QuantumRegister
|
||||
from qiskit.circuit import ClassicalRegister, Clbit
|
||||
from qiskit.circuit import QuantumCircuit
|
||||
from qiskit.circuit import Measure
|
||||
|
@ -77,8 +77,8 @@ def raise_if_dagcircuit_invalid(dag):
|
|||
if edge_data['wire'] not in dag.wires]
|
||||
if edges_outside_wires:
|
||||
raise DAGCircuitError('multi_graph contains one or more edges ({}) '
|
||||
'not found in DAGCircuit.wires ({}).'.format(
|
||||
edges_outside_wires, dag.wires))
|
||||
'not found in DAGCircuit.wires ({}).'.format(edges_outside_wires,
|
||||
dag.wires))
|
||||
|
||||
# Every wire should have exactly one input node and one output node.
|
||||
for wire in dag.wires:
|
||||
|
@ -147,12 +147,12 @@ class TestDagRegisters(QiskitTestCase):
|
|||
dag.add_qreg(QuantumRegister(1, 'qr3'))
|
||||
dag.add_qreg(QuantumRegister(1, 'qr4'))
|
||||
dag.add_qreg(QuantumRegister(1, 'qr6'))
|
||||
self.assertListEqual(dag.qubits(), [(QuantumRegister(1, 'qr1'), 0),
|
||||
(QuantumRegister(1, 'qr10'), 0),
|
||||
(QuantumRegister(1, 'qr0'), 0),
|
||||
(QuantumRegister(1, 'qr3'), 0),
|
||||
(QuantumRegister(1, 'qr4'), 0),
|
||||
(QuantumRegister(1, 'qr6'), 0)])
|
||||
self.assertListEqual(dag.qubits(), [QuantumRegister(1, 'qr1')[0],
|
||||
QuantumRegister(1, 'qr10')[0],
|
||||
QuantumRegister(1, 'qr0')[0],
|
||||
QuantumRegister(1, 'qr3')[0],
|
||||
QuantumRegister(1, 'qr4')[0],
|
||||
QuantumRegister(1, 'qr6')[0]])
|
||||
|
||||
def test_add_reg_duplicate(self):
|
||||
"""add_qreg with the same register twice is not allowed."""
|
||||
|
@ -222,22 +222,22 @@ class TestDagOperations(QiskitTestCase):
|
|||
sorted(self.dag._multi_graph.in_edges(h_node, data=True)),
|
||||
sorted([
|
||||
(self.dag.input_map[self.qubit2], h_node,
|
||||
{'wire': Qubit(*self.qubit2), 'name': 'qr[2]'}),
|
||||
{'wire': self.qubit2, 'name': 'qr[2]'}),
|
||||
(self.dag.input_map[self.clbit0], h_node,
|
||||
{'wire': Clbit(*self.clbit0), 'name': 'cr[0]'}),
|
||||
{'wire': self.clbit0, 'name': 'cr[0]'}),
|
||||
(self.dag.input_map[self.clbit1], h_node,
|
||||
{'wire': Clbit(*self.clbit1), 'name': 'cr[1]'}),
|
||||
{'wire': self.clbit1, 'name': 'cr[1]'}),
|
||||
]))
|
||||
|
||||
self.assertEqual(
|
||||
sorted(self.dag._multi_graph.out_edges(h_node, data=True)),
|
||||
sorted([
|
||||
(h_node, self.dag.output_map[self.qubit2],
|
||||
{'wire': Qubit(*self.qubit2), 'name': 'qr[2]'}),
|
||||
{'wire': self.qubit2, 'name': 'qr[2]'}),
|
||||
(h_node, self.dag.output_map[self.clbit0],
|
||||
{'wire': Clbit(*self.clbit0), 'name': 'cr[0]'}),
|
||||
{'wire': self.clbit0, 'name': 'cr[0]'}),
|
||||
(h_node, self.dag.output_map[self.clbit1],
|
||||
{'wire': Clbit(*self.clbit1), 'name': 'cr[1]'}),
|
||||
{'wire': self.clbit1, 'name': 'cr[1]'}),
|
||||
]))
|
||||
|
||||
self.assertTrue(nx.is_directed_acyclic_graph(self.dag._multi_graph))
|
||||
|
@ -264,9 +264,9 @@ class TestDagOperations(QiskitTestCase):
|
|||
sorted(self.dag._multi_graph.in_edges(meas_node, data=True)),
|
||||
sorted([
|
||||
(self.dag.input_map[self.qubit0], meas_node,
|
||||
{'wire': Qubit(*self.qubit0), 'name': 'qr[0]'}),
|
||||
{'wire': self.qubit0, 'name': 'qr[0]'}),
|
||||
(self.dag.input_map[self.clbit0], meas_node,
|
||||
{'wire': Clbit(*self.clbit0), 'name': 'cr[0]'}),
|
||||
{'wire': self.clbit0, 'name': 'cr[0]'}),
|
||||
(self.dag.input_map[new_creg[0]], meas_node,
|
||||
{'wire': Clbit(new_creg, 0), 'name': 'cr2[0]'}),
|
||||
]))
|
||||
|
@ -275,9 +275,9 @@ class TestDagOperations(QiskitTestCase):
|
|||
sorted(self.dag._multi_graph.out_edges(meas_node, data=True)),
|
||||
sorted([
|
||||
(meas_node, self.dag.output_map[self.qubit0],
|
||||
{'wire': Qubit(*self.qubit0), 'name': 'qr[0]'}),
|
||||
{'wire': self.qubit0, 'name': 'qr[0]'}),
|
||||
(meas_node, self.dag.output_map[self.clbit0],
|
||||
{'wire': Clbit(*self.clbit0), 'name': 'cr[0]'}),
|
||||
{'wire': self.clbit0, 'name': 'cr[0]'}),
|
||||
(meas_node, self.dag.output_map[new_creg[0]],
|
||||
{'wire': Clbit(new_creg, 0), 'name': 'cr2[0]'}),
|
||||
]))
|
||||
|
@ -303,22 +303,22 @@ class TestDagOperations(QiskitTestCase):
|
|||
sorted(self.dag._multi_graph.in_edges(meas_node, data=True)),
|
||||
sorted([
|
||||
(self.dag.input_map[self.qubit1], meas_node,
|
||||
{'wire': Qubit(*self.qubit1), 'name': 'qr[1]'}),
|
||||
{'wire': self.qubit1, 'name': 'qr[1]'}),
|
||||
(self.dag.input_map[self.clbit0], meas_node,
|
||||
{'wire': Clbit(*self.clbit0), 'name': 'cr[0]'}),
|
||||
{'wire': self.clbit0, 'name': 'cr[0]'}),
|
||||
(self.dag.input_map[self.clbit1], meas_node,
|
||||
{'wire': Clbit(*self.clbit1), 'name': 'cr[1]'}),
|
||||
{'wire': self.clbit1, 'name': 'cr[1]'}),
|
||||
]))
|
||||
|
||||
self.assertEqual(
|
||||
sorted(self.dag._multi_graph.out_edges(meas_node, data=True)),
|
||||
sorted([
|
||||
(meas_node, self.dag.output_map[self.qubit1],
|
||||
{'wire': Qubit(*self.qubit1), 'name': 'qr[1]'}),
|
||||
{'wire': self.qubit1, 'name': 'qr[1]'}),
|
||||
(meas_node, self.dag.output_map[self.clbit0],
|
||||
{'wire': Clbit(*self.clbit0), 'name': 'cr[0]'}),
|
||||
{'wire': self.clbit0, 'name': 'cr[0]'}),
|
||||
(meas_node, self.dag.output_map[self.clbit1],
|
||||
{'wire': Clbit(*self.clbit1), 'name': 'cr[1]'}),
|
||||
{'wire': self.clbit1, 'name': 'cr[1]'}),
|
||||
]))
|
||||
|
||||
self.assertTrue(nx.is_directed_acyclic_graph(self.dag._multi_graph))
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
|
||||
import copy
|
||||
import unittest
|
||||
import warnings
|
||||
import numpy
|
||||
|
||||
from qiskit.circuit import QuantumRegister, Qubit
|
||||
|
@ -96,7 +95,7 @@ class LayoutTest(QiskitTestCase):
|
|||
def test_layout_avoid_dangling_virtual(self):
|
||||
""" No dangling pointers for virtual qubits."""
|
||||
layout = Layout({self.qr[0]: 0})
|
||||
self.assertEqual(layout[0], (self.qr, 0))
|
||||
self.assertEqual(layout[0], self.qr[0])
|
||||
layout[0] = self.qr[1]
|
||||
with self.assertRaises(KeyError):
|
||||
_ = layout[self.qr[0]]
|
||||
|
@ -176,7 +175,7 @@ class LayoutTest(QiskitTestCase):
|
|||
layout.add(self.qr[0])
|
||||
layout.add(self.qr[1])
|
||||
layout.swap(0, 1)
|
||||
self.assertDictEqual(layout.get_virtual_bits(), {(self.qr, 0): 1, (self.qr, 1): 0})
|
||||
self.assertDictEqual(layout.get_virtual_bits(), {self.qr[0]: 1, self.qr[1]: 0})
|
||||
|
||||
def test_layout_swap_error(self):
|
||||
"""swap() method error"""
|
||||
|
@ -184,7 +183,7 @@ class LayoutTest(QiskitTestCase):
|
|||
layout.add(self.qr[0])
|
||||
layout.add(self.qr[1])
|
||||
with self.assertRaises(LayoutError):
|
||||
layout.swap(0, (self.qr, 0))
|
||||
layout.swap(0, self.qr[0])
|
||||
|
||||
def test_layout_combine(self):
|
||||
"""combine_into_edge_map() method"""
|
||||
|
@ -196,7 +195,7 @@ class LayoutTest(QiskitTestCase):
|
|||
another_layout.add(self.qr[0])
|
||||
|
||||
edge_map = layout.combine_into_edge_map(another_layout)
|
||||
self.assertDictEqual(edge_map, {(self.qr, 0): (self.qr, 1), (self.qr, 1): (self.qr, 0)})
|
||||
self.assertDictEqual(edge_map, {self.qr[0]: self.qr[1], self.qr[1]: self.qr[0]})
|
||||
|
||||
def test_layout_combine_bigger(self):
|
||||
"""combine_into_edge_map() method with another_layout is bigger"""
|
||||
|
@ -209,7 +208,7 @@ class LayoutTest(QiskitTestCase):
|
|||
another_layout.add(self.qr[2])
|
||||
|
||||
edge_map = layout.combine_into_edge_map(another_layout)
|
||||
self.assertDictEqual(edge_map, {(self.qr, 0): (self.qr, 1), (self.qr, 1): (self.qr, 0)})
|
||||
self.assertDictEqual(edge_map, {self.qr[0]: self.qr[1], self.qr[1]: self.qr[0]})
|
||||
|
||||
def test_set_virtual_without_physical(self):
|
||||
"""When adding a virtual without care in which physical is going"""
|
||||
|
@ -217,7 +216,7 @@ class LayoutTest(QiskitTestCase):
|
|||
layout.add(self.qr[1], 2)
|
||||
layout.add(self.qr[0])
|
||||
|
||||
self.assertDictEqual(layout.get_virtual_bits(), {(self.qr, 0): 1, (self.qr, 1): 2})
|
||||
self.assertDictEqual(layout.get_virtual_bits(), {self.qr[0]: 1, self.qr[1]: 2})
|
||||
|
||||
def test_layout_combine_smaller(self):
|
||||
"""combine_into_edge_map() method with another_layout is smaller and raises an Error"""
|
||||
|
@ -418,385 +417,5 @@ class LayoutTest(QiskitTestCase):
|
|||
self.assertDictEqual(layout._v2p, expected._v2p)
|
||||
|
||||
|
||||
class LayoutDeprecatedTest(QiskitTestCase):
|
||||
"""Test layout object with tuples, to be deprecated.
|
||||
(to be removed when _cast_tuple_to_bit gets removed)"""
|
||||
|
||||
def setUp(self):
|
||||
self.qr = QuantumRegister(3, 'qr')
|
||||
|
||||
# Disable specific DeprecationWarning from this TestCase.
|
||||
warnings.filterwarnings('ignore', category=DeprecationWarning,
|
||||
module='qiskit.transpiler.layout')
|
||||
|
||||
def test_default_layout(self):
|
||||
"""Static method generate_trivial_layout creates a Layout"""
|
||||
qr0 = QuantumRegister(3, 'q0')
|
||||
qr1 = QuantumRegister(2, 'qr1')
|
||||
layout = Layout.generate_trivial_layout(qr0, qr1)
|
||||
|
||||
self.assertEqual(layout[(qr0, 0)], 0)
|
||||
self.assertEqual(layout[(qr0, 1)], 1)
|
||||
self.assertEqual(layout[(qr0, 2)], 2)
|
||||
self.assertEqual(layout[(qr1, 0)], 3)
|
||||
self.assertEqual(layout[(qr1, 1)], 4)
|
||||
|
||||
def test_layout_from_dict(self):
|
||||
"""Constructor from a dict"""
|
||||
layout = Layout({(self.qr, 0): 0,
|
||||
(self.qr, 1): 1,
|
||||
(self.qr, 2): 2})
|
||||
|
||||
self.assertEqual(layout[(self.qr, 0)], 0)
|
||||
self.assertEqual(layout[(self.qr, 1)], 1)
|
||||
self.assertEqual(layout[(self.qr, 2)], 2)
|
||||
self.assertEqual(layout[0], (self.qr, 0))
|
||||
self.assertEqual(layout[1], (self.qr, 1))
|
||||
self.assertEqual(layout[2], (self.qr, 2))
|
||||
|
||||
def test_layout_from_dict_hole(self):
|
||||
"""Constructor from a dict with a hole"""
|
||||
qr0 = QuantumRegister(2)
|
||||
qr1 = QuantumRegister(2)
|
||||
|
||||
layout = Layout({qr0[0]: 0,
|
||||
qr1[0]: 1,
|
||||
qr1[1]: 3,
|
||||
qr0[1]: 4})
|
||||
|
||||
self.assertEqual(layout[qr0[0]], 0)
|
||||
self.assertEqual(layout[qr1[0]], 1)
|
||||
with self.assertRaises(KeyError):
|
||||
_ = layout[None]
|
||||
self.assertEqual(layout[qr1[1]], 3)
|
||||
self.assertEqual(layout[qr0[1]], 4)
|
||||
|
||||
self.assertEqual(layout[0], qr0[0])
|
||||
self.assertEqual(layout[1], qr1[0])
|
||||
self.assertEqual(layout[3], qr1[1])
|
||||
self.assertEqual(layout[4], qr0[1])
|
||||
|
||||
def test_layout_set(self):
|
||||
"""Setter"""
|
||||
layout = Layout()
|
||||
layout[(self.qr, 0)] = 0
|
||||
self.assertEqual(layout[(self.qr, 0)], 0)
|
||||
self.assertEqual(layout[0], (self.qr, 0))
|
||||
|
||||
def test_layout_avoid_dangling_physical(self):
|
||||
""" No dangling pointers for physical qubits."""
|
||||
layout = Layout({(self.qr, 0): 0})
|
||||
self.assertEqual(layout[0], (self.qr, 0))
|
||||
layout[(self.qr, 0)] = 1
|
||||
with self.assertRaises(KeyError):
|
||||
_ = layout[0]
|
||||
|
||||
def test_layout_avoid_dangling_virtual(self):
|
||||
""" No dangling pointers for virtual qubits."""
|
||||
layout = Layout({(self.qr, 0): 0})
|
||||
self.assertEqual(layout[0], (self.qr, 0))
|
||||
layout[0] = (self.qr, 1)
|
||||
with self.assertRaises(KeyError):
|
||||
_ = layout[(self.qr, 0)]
|
||||
|
||||
def test_layout_len(self):
|
||||
"""Length of the layout is the amount of physical bits"""
|
||||
layout = Layout()
|
||||
self.assertEqual(len(layout), 0)
|
||||
layout.add((self.qr, 2))
|
||||
self.assertEqual(len(layout), 1)
|
||||
layout.add((self.qr, 1), 3)
|
||||
self.assertEqual(len(layout), 2)
|
||||
|
||||
def test_layout_len_with_idle(self):
|
||||
"""Length of the layout is the amount of physical bits"""
|
||||
layout = Layout()
|
||||
self.assertEqual(len(layout), 0)
|
||||
layout.add((self.qr, 2))
|
||||
self.assertEqual(len(layout), 1)
|
||||
layout.add((self.qr, 1), 3)
|
||||
self.assertEqual(len(layout), 2)
|
||||
|
||||
def test_layout_get_bits(self):
|
||||
"""Get the map from the (qu)bits view"""
|
||||
layout_dict = {(self.qr, 0): 0,
|
||||
(self.qr, 1): 1,
|
||||
(self.qr, 2): 2}
|
||||
layout = Layout(layout_dict)
|
||||
self.assertDictEqual(layout_dict, layout.get_virtual_bits())
|
||||
|
||||
def test_layout_get_physical_bits(self):
|
||||
"""Get the map from the physical bits view"""
|
||||
layout = Layout({(self.qr, 0): 0, (self.qr, 1): 1, (self.qr, 2): 2})
|
||||
self.assertDictEqual(layout.get_physical_bits(), {0: (self.qr, 0),
|
||||
1: (self.qr, 1),
|
||||
2: (self.qr, 2)})
|
||||
|
||||
def test_layout_add(self):
|
||||
"""add() method"""
|
||||
layout = Layout()
|
||||
layout[(self.qr, 0)] = 0
|
||||
layout.add((self.qr, 1))
|
||||
|
||||
self.assertEqual(layout[(self.qr, 1)], 1)
|
||||
self.assertEqual(layout[1], (self.qr, 1))
|
||||
|
||||
def test_layout_add_register(self):
|
||||
"""add_register() method"""
|
||||
layout = Layout()
|
||||
layout.add_register(QuantumRegister(2, 'q0'))
|
||||
layout.add_register(QuantumRegister(1, 'qr1'))
|
||||
|
||||
self.assertEqual(layout[(QuantumRegister(2, 'q0'), 0)], 0)
|
||||
self.assertEqual(layout[(QuantumRegister(2, 'q0'), 1)], 1)
|
||||
self.assertEqual(layout[(QuantumRegister(1, 'qr1'), 0)], 2)
|
||||
|
||||
def test_physical_keyerror(self):
|
||||
"""When asking for an unexistant physical qubit, KeyError"""
|
||||
layout = Layout()
|
||||
layout[(self.qr, 0)] = 1
|
||||
|
||||
with self.assertRaises(KeyError):
|
||||
_ = layout[0]
|
||||
|
||||
def test_virtual_keyerror(self):
|
||||
"""When asking for an unexistant virtual qubit, KeyError"""
|
||||
layout = Layout()
|
||||
layout[(self.qr, 0)] = 1
|
||||
|
||||
with self.assertRaises(KeyError):
|
||||
_ = layout[(self.qr, 1)]
|
||||
|
||||
def test_layout_swap(self):
|
||||
"""swap() method"""
|
||||
layout = Layout()
|
||||
layout.add((self.qr, 0))
|
||||
layout.add((self.qr, 1))
|
||||
layout.swap(0, 1)
|
||||
self.assertDictEqual(layout.get_virtual_bits(), {(self.qr, 0): 1, (self.qr, 1): 0})
|
||||
|
||||
def test_layout_swap_error(self):
|
||||
"""swap() method error"""
|
||||
layout = Layout()
|
||||
layout.add((self.qr, 0))
|
||||
layout.add((self.qr, 1))
|
||||
with self.assertRaises(LayoutError):
|
||||
layout.swap(0, (self.qr, 0))
|
||||
|
||||
def test_layout_combine(self):
|
||||
"""combine_into_edge_map() method"""
|
||||
layout = Layout()
|
||||
layout.add((self.qr, 0))
|
||||
layout.add((self.qr, 1))
|
||||
another_layout = Layout()
|
||||
another_layout.add((self.qr, 1))
|
||||
another_layout.add((self.qr, 0))
|
||||
|
||||
edge_map = layout.combine_into_edge_map(another_layout)
|
||||
self.assertDictEqual(edge_map, {(self.qr, 0): (self.qr, 1), (self.qr, 1): (self.qr, 0)})
|
||||
|
||||
def test_layout_combine_bigger(self):
|
||||
"""combine_into_edge_map() method with another_layout is bigger"""
|
||||
layout = Layout()
|
||||
layout.add((self.qr, 0))
|
||||
layout.add((self.qr, 1))
|
||||
another_layout = Layout()
|
||||
another_layout.add((self.qr, 1))
|
||||
another_layout.add((self.qr, 0))
|
||||
another_layout.add((self.qr, 2))
|
||||
|
||||
edge_map = layout.combine_into_edge_map(another_layout)
|
||||
self.assertDictEqual(edge_map, {(self.qr, 0): (self.qr, 1), (self.qr, 1): (self.qr, 0)})
|
||||
|
||||
def test_set_virtual_without_physical(self):
|
||||
"""When adding a virtual without care in which physical is going"""
|
||||
layout = Layout()
|
||||
layout.add((self.qr, 1), 2)
|
||||
layout.add((self.qr, 0))
|
||||
|
||||
self.assertDictEqual(layout.get_virtual_bits(), {(self.qr, 0): 1, (self.qr, 1): 2})
|
||||
|
||||
def test_layout_combine_smaller(self):
|
||||
"""combine_into_edge_map() method with another_layout is smaller and raises an Error"""
|
||||
layout = Layout()
|
||||
layout.add((self.qr, 0))
|
||||
layout.add((self.qr, 1))
|
||||
layout.add((self.qr, 2))
|
||||
another_layout = Layout()
|
||||
another_layout.add((self.qr, 1))
|
||||
another_layout.add((self.qr, 0))
|
||||
|
||||
with self.assertRaises(LayoutError):
|
||||
_ = layout.combine_into_edge_map(another_layout)
|
||||
|
||||
def test_copy(self):
|
||||
"""Test copy methods return equivalent layouts."""
|
||||
layout = Layout()
|
||||
layout.add((self.qr, 0))
|
||||
layout.add((self.qr, 1))
|
||||
|
||||
layout_dict_copy = layout.copy()
|
||||
self.assertTrue(isinstance(layout_dict_copy, Layout))
|
||||
self.assertDictEqual(layout.get_physical_bits(), layout_dict_copy.get_physical_bits())
|
||||
self.assertDictEqual(layout.get_virtual_bits(), layout_dict_copy.get_virtual_bits())
|
||||
|
||||
layout_copy_copy = copy.copy(layout)
|
||||
self.assertTrue(isinstance(layout_copy_copy, Layout))
|
||||
self.assertDictEqual(layout.get_physical_bits(), layout_dict_copy.get_physical_bits())
|
||||
self.assertDictEqual(layout.get_virtual_bits(), layout_dict_copy.get_virtual_bits())
|
||||
|
||||
layout_copy_deepcopy = copy.deepcopy(layout)
|
||||
self.assertTrue(isinstance(layout_copy_deepcopy, Layout))
|
||||
self.assertDictEqual(layout.get_physical_bits(), layout_dict_copy.get_physical_bits())
|
||||
self.assertDictEqual(layout.get_virtual_bits(), layout_dict_copy.get_virtual_bits())
|
||||
|
||||
def test_layout_error_str_key(self):
|
||||
"""Layout does not work with strings"""
|
||||
layout = Layout()
|
||||
|
||||
with self.assertRaises(LayoutError):
|
||||
layout['a_string'] = 3
|
||||
|
||||
with self.assertRaises(LayoutError):
|
||||
layout[2] = 'a_string'
|
||||
|
||||
def test_layout_error_when_same_type(self):
|
||||
"""Layout does not work when key and value are the same type"""
|
||||
layout = Layout()
|
||||
|
||||
with self.assertRaises(LayoutError):
|
||||
layout[(self.qr, 0)] = (self.qr, 1)
|
||||
|
||||
with self.assertRaises(LayoutError):
|
||||
layout[0] = 1
|
||||
|
||||
def test_layout_repr(self):
|
||||
"""Layout repr reproduces layout"""
|
||||
qr = QuantumRegister(5, 'qr')
|
||||
layout = Layout({(qr, 0): 2,
|
||||
(qr, 1): 4,
|
||||
(qr, 2): 3,
|
||||
(qr, 3): 0,
|
||||
(qr, 4): 1,
|
||||
})
|
||||
|
||||
repr_layout = eval(layout.__repr__()) # pylint: disable=eval-used
|
||||
self.assertDictEqual(layout._p2v, repr_layout._p2v)
|
||||
self.assertDictEqual(layout._v2p, repr_layout._v2p)
|
||||
|
||||
def test_layout_repr_with_holes(self):
|
||||
"""A non-bijective Layout repr reproduces layout"""
|
||||
qr = QuantumRegister(5, 'qr')
|
||||
layout = Layout({qr[0]: 0, qr[1]: 3, qr[2]: 4, qr[3]: 5, qr[4]: 6})
|
||||
|
||||
repr_layout = eval(layout.__repr__()) # pylint: disable=eval-used
|
||||
self.assertDictEqual(layout._p2v, repr_layout._p2v)
|
||||
self.assertDictEqual(layout._v2p, repr_layout._v2p)
|
||||
|
||||
def test_layout_from_intlist(self):
|
||||
"""Create a layout from a list of integers.
|
||||
virtual physical
|
||||
q1_0 -> 4
|
||||
q2_0 -> 5
|
||||
q2_1 -> 6
|
||||
q3_0 -> 8
|
||||
q3_1 -> 9
|
||||
q3_2 -> 10
|
||||
"""
|
||||
qr1 = QuantumRegister(1, 'qr1')
|
||||
qr2 = QuantumRegister(2, 'qr2')
|
||||
qr3 = QuantumRegister(3, 'qr3')
|
||||
intlist_layout = [4, 5, 6, 8, 9, 10]
|
||||
layout = Layout.from_intlist(intlist_layout, qr1, qr2, qr3)
|
||||
|
||||
expected = Layout({4: qr1[0],
|
||||
5: qr2[0],
|
||||
6: qr2[1],
|
||||
8: qr3[0],
|
||||
9: qr3[1],
|
||||
10: qr3[2]
|
||||
})
|
||||
self.assertDictEqual(layout._p2v, expected._p2v)
|
||||
self.assertDictEqual(layout._v2p, expected._v2p)
|
||||
|
||||
def test_layout_from_intlist_short(self):
|
||||
"""If the intlist is longer that your quantum register, map them to None.
|
||||
virtual physical
|
||||
q1_0 -> 4
|
||||
q2_0 -> 5
|
||||
q2_1 -> 6
|
||||
None -> 8
|
||||
None -> 9
|
||||
None -> 10
|
||||
"""
|
||||
qr1 = QuantumRegister(1, 'qr1')
|
||||
qr2 = QuantumRegister(2, 'qr2')
|
||||
|
||||
intlist_layout = [4, 5, 6, 8, 9, 10]
|
||||
layout = Layout.from_intlist(intlist_layout, qr1, qr2)
|
||||
|
||||
expected = Layout({4: qr1[0],
|
||||
5: qr2[0],
|
||||
6: qr2[1],
|
||||
8: None,
|
||||
9: None,
|
||||
10: None
|
||||
})
|
||||
self.assertDictEqual(layout._p2v, expected._p2v)
|
||||
self.assertDictEqual(layout._v2p, expected._v2p)
|
||||
|
||||
def test_layout_from_intlist_long(self):
|
||||
"""If the intlist is shorter that your quantum register, fail.
|
||||
virtual physical
|
||||
q1_0 -> 4
|
||||
q2_0 -> 5
|
||||
q2_1 -> 6
|
||||
q3_0 -> 8
|
||||
q3_1 -> ?
|
||||
q3_2 -> ?
|
||||
"""
|
||||
qr1 = QuantumRegister(1, 'qr1')
|
||||
qr2 = QuantumRegister(2, 'qr2')
|
||||
qr3 = QuantumRegister(3, 'qr3')
|
||||
intlist_layout = [4, 5, 6, 8]
|
||||
|
||||
with self.assertRaises(LayoutError):
|
||||
_ = Layout.from_intlist(intlist_layout, qr1, qr2, qr3)
|
||||
|
||||
def test_layout_from_intlist_duplicated(self):
|
||||
"""If the intlist contains duplicated ints, fail.
|
||||
virtual physical
|
||||
q1_0 -> 4
|
||||
q2_0 -> 6 -- This is
|
||||
q2_1 -> 6 -- not allowed
|
||||
"""
|
||||
qr1 = QuantumRegister(1, 'qr1')
|
||||
qr2 = QuantumRegister(2, 'qr2')
|
||||
intlist_layout = [4, 6, 6]
|
||||
|
||||
with self.assertRaises(LayoutError):
|
||||
_ = Layout.from_intlist(intlist_layout, qr1, qr2)
|
||||
|
||||
def test_layout_from_tuplelist(self):
|
||||
"""Create a Layout from list of tuples
|
||||
virtual physical
|
||||
q1_0 -> 3
|
||||
q2_0 -> 5
|
||||
q2_1 -> 7
|
||||
"""
|
||||
qr1 = QuantumRegister(1, 'qr1')
|
||||
qr2 = QuantumRegister(2, 'qr2')
|
||||
tuplelist_layout = [None, None, None, qr1[0], None, qr2[0], None, qr2[1]]
|
||||
|
||||
layout = Layout.from_tuplelist(tuplelist_layout)
|
||||
|
||||
expected = Layout({3: qr1[0],
|
||||
5: qr2[0],
|
||||
7: qr2[1],
|
||||
})
|
||||
self.assertDictEqual(layout._p2v, expected._p2v)
|
||||
self.assertDictEqual(layout._v2p, expected._v2p)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
|
@ -118,13 +118,9 @@ class TestLookaheadSwap(QiskitTestCase):
|
|||
|
||||
mapped_dag = LookaheadSwap(coupling_map).run(dag_circuit)
|
||||
|
||||
mapped_measure_qargs = set(op.qargs[0]
|
||||
mapped_measure_qargs = set(op.qargs[0] for op in mapped_dag.named_nodes('measure'))
|
||||
|
||||
for op in mapped_dag.named_nodes('measure'))
|
||||
|
||||
self.assertIn(mapped_measure_qargs,
|
||||
[set(((QuantumRegister(3, 'q'), 0), (QuantumRegister(3, 'q'), 1))),
|
||||
set(((QuantumRegister(3, 'q'), 1), (QuantumRegister(3, 'q'), 2)))])
|
||||
self.assertIn(mapped_measure_qargs, [set((qr[0], qr[1])), set((qr[1], qr[2]))])
|
||||
|
||||
def test_lookahead_swap_maps_barriers(self):
|
||||
"""Verify barrier nodes are updated to re-mapped qregs.
|
||||
|
@ -149,13 +145,9 @@ class TestLookaheadSwap(QiskitTestCase):
|
|||
|
||||
mapped_dag = LookaheadSwap(coupling_map).run(dag_circuit)
|
||||
|
||||
mapped_barrier_qargs = [set(op.qargs)
|
||||
mapped_barrier_qargs = [set(op.qargs) for op in mapped_dag.named_nodes('barrier')][0]
|
||||
|
||||
for op in mapped_dag.named_nodes('barrier')][0]
|
||||
|
||||
self.assertIn(mapped_barrier_qargs,
|
||||
[set(((QuantumRegister(3, 'q'), 0), (QuantumRegister(3, 'q'), 1))),
|
||||
set(((QuantumRegister(3, 'q'), 1), (QuantumRegister(3, 'q'), 2)))])
|
||||
self.assertIn(mapped_barrier_qargs, [set((qr[0], qr[1])), set((qr[1], qr[2]))])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue