From 8e717e66aab9adc4d691f30b659bdb95395a27dc Mon Sep 17 00:00:00 2001 From: georgios-ts Date: Fri, 2 Oct 2020 12:36:37 +0300 Subject: [PATCH 1/4] build blueprintcircuit before inverse and compose --- qiskit/circuit/library/blueprintcircuit.py | 10 ++++++++++ .../circuit/library/test_blueprintcircuit.py | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/qiskit/circuit/library/blueprintcircuit.py b/qiskit/circuit/library/blueprintcircuit.py index 958b1b65c2db..7d339f11ae3e 100644 --- a/qiskit/circuit/library/blueprintcircuit.py +++ b/qiskit/circuit/library/blueprintcircuit.py @@ -100,6 +100,16 @@ def append(self, instruction, qargs=None, cargs=None): self._build() return super().append(instruction, qargs, cargs) + def compose(self, other, qubits=None, clbits=None, front=False, inplace=False): + if self._data is None: + self._build() + return super().compose(other, qubits, clbits, front, inplace) + + def inverse(self): + if self._data is None: + self._build() + return super().inverse() + def __len__(self): return len(self.data) diff --git a/test/python/circuit/library/test_blueprintcircuit.py b/test/python/circuit/library/test_blueprintcircuit.py index 36e42f640285..e91d6cabdef8 100644 --- a/test/python/circuit/library/test_blueprintcircuit.py +++ b/test/python/circuit/library/test_blueprintcircuit.py @@ -15,7 +15,7 @@ import unittest from qiskit.test.base import QiskitTestCase -from qiskit.circuit import QuantumRegister, Parameter +from qiskit.circuit import QuantumRegister, Parameter, QuantumCircuit from qiskit.circuit.library import BlueprintCircuit @@ -90,7 +90,7 @@ def test_calling_attributes_works(self): self.assertGreater(len(circuit._data), 0) methods = ['qasm', 'count_ops', 'num_connected_components', 'num_nonlocal_gates', - 'depth', '__len__', 'copy'] + 'depth', '__len__', 'copy', 'inverse'] for method in methods: with self.subTest(method=method): circuit = MockBlueprint(3) @@ -102,6 +102,19 @@ def test_calling_attributes_works(self): _ = circuit[2] self.assertGreater(len(circuit._data), 0) + def test_compose_works(self): + """Test that the circuit is constructed when compose is called.""" + qc = QuantumCircuit(3) + qc.x([0, 1, 2]) + circuit = MockBlueprint(3) + circuit.compose(qc, inplace=True) + + reference = QuantumCircuit(3) + reference.rx(list(circuit.parameters)[0], 0) + reference.h([0, 1, 2]) + reference.x([0, 1, 2]) + + self.assertEqual(reference, circuit) if __name__ == '__main__': unittest.main() From 7d5045bda3e1b4f6cd59e008e5860b5a832c5bac Mon Sep 17 00:00:00 2001 From: georgios-ts Date: Fri, 2 Oct 2020 13:21:06 +0300 Subject: [PATCH 2/4] lint --- test/python/circuit/library/test_blueprintcircuit.py | 1 + 1 file changed, 1 insertion(+) diff --git a/test/python/circuit/library/test_blueprintcircuit.py b/test/python/circuit/library/test_blueprintcircuit.py index e91d6cabdef8..701085541e00 100644 --- a/test/python/circuit/library/test_blueprintcircuit.py +++ b/test/python/circuit/library/test_blueprintcircuit.py @@ -116,5 +116,6 @@ def test_compose_works(self): self.assertEqual(reference, circuit) + if __name__ == '__main__': unittest.main() From 4f7738a1c281d10dd9996f0413ab8f77dcf0af3a Mon Sep 17 00:00:00 2001 From: georgios-ts Date: Sat, 3 Oct 2020 17:09:43 +0300 Subject: [PATCH 3/4] add releasenote --- ...ld-blueprintcircuit-before-inverse-8ef85f6389588fe0.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 releasenotes/notes/build-blueprintcircuit-before-inverse-8ef85f6389588fe0.yaml diff --git a/releasenotes/notes/build-blueprintcircuit-before-inverse-8ef85f6389588fe0.yaml b/releasenotes/notes/build-blueprintcircuit-before-inverse-8ef85f6389588fe0.yaml new file mode 100644 index 000000000000..0b6d9b0feaea --- /dev/null +++ b/releasenotes/notes/build-blueprintcircuit-before-inverse-8ef85f6389588fe0.yaml @@ -0,0 +1,6 @@ +--- +fixes: + - | + Calling inverse or compose on a ``BlueprintCircuit`` could fail + if ``_data`` was not yet popualted. This has been fixed by asserting + a call on ``_build`` method. From 38180f47ef17e376c1a7abeeeee5724ba01d170c Mon Sep 17 00:00:00 2001 From: georgios-ts Date: Sat, 3 Oct 2020 17:11:28 +0300 Subject: [PATCH 4/4] fix typo --- .../build-blueprintcircuit-before-inverse-8ef85f6389588fe0.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/releasenotes/notes/build-blueprintcircuit-before-inverse-8ef85f6389588fe0.yaml b/releasenotes/notes/build-blueprintcircuit-before-inverse-8ef85f6389588fe0.yaml index 0b6d9b0feaea..a8be8850b7b3 100644 --- a/releasenotes/notes/build-blueprintcircuit-before-inverse-8ef85f6389588fe0.yaml +++ b/releasenotes/notes/build-blueprintcircuit-before-inverse-8ef85f6389588fe0.yaml @@ -2,5 +2,5 @@ fixes: - | Calling inverse or compose on a ``BlueprintCircuit`` could fail - if ``_data`` was not yet popualted. This has been fixed by asserting + if ``_data`` was not yet populated. This has been fixed by asserting a call on ``_build`` method.