Skip to content

Commit

Permalink
Fix bug in circuit decompose (#8364)
Browse files Browse the repository at this point in the history
* fix bug in circuit decompose

* add test

* Update test/python/circuit/test_circuit_operations.py

Co-authored-by: Matthew Treinish <[email protected]>

Co-authored-by: Matthew Treinish <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Jul 19, 2022
1 parent 6dd0d69 commit 7753560
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
4 changes: 1 addition & 3 deletions qiskit/transpiler/passes/basis/decompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,7 @@ def _should_decompose(self, node) -> bool:
node.name in gates or any(fnmatch(node.name, p) for p in strings_list)
):
return True
elif not has_label and ( # check if Gate type given
any(isinstance(node.op, op) for op in gate_type_list)
):
elif any(isinstance(node.op, op) for op in gate_type_list): # check if Gate type given
return True
else:
return False
4 changes: 4 additions & 0 deletions releasenotes/notes/decompose-fix-993f7242eaa69407.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- Fixed a bug in :meth:`.QuantumCircuit.decompose` that caused the
`gates_to_decompose` argument to be handled incorrectly.
7 changes: 7 additions & 0 deletions test/python/circuit/test_circuit_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -1265,3 +1265,10 @@ def test_pop_previous_instruction_removes_parameters(self):
instruction = test._pop_previous_instruction_in_scope()
self.assertEqual(list(last_instructions), [instruction])
self.assertEqual({y}, set(test.parameters))

def test_decompose_gate_type(self):
"""Test decompose specifying gate type."""
circuit = QuantumCircuit(1)
circuit.append(SGate(label="s_gate"), [0])
decomposed = circuit.decompose(gates_to_decompose=SGate)
self.assertNotIn("s", decomposed.count_ops())

0 comments on commit 7753560

Please sign in to comment.