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

Build BlueprintCircuit before inverse and compose #5170

Merged
merged 6 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions qiskit/circuit/library/blueprintcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Calling inverse or compose on a ``BlueprintCircuit`` could fail
if ``_data`` was not yet populated. This has been fixed by asserting
a call on ``_build`` method.
18 changes: 16 additions & 2 deletions test/python/circuit/library/test_blueprintcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand All @@ -102,6 +102,20 @@ 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()