Skip to content

Commit

Permalink
Fix wrapping of GroverOperator (#6697)
Browse files Browse the repository at this point in the history
* barrier isn't good enough to check!

* add test

Co-authored-by: Manoel Marques <[email protected]>
  • Loading branch information
Cryoris and manoelmarques authored Jul 8, 2021
1 parent d83ce48 commit ecde497
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 4 additions & 3 deletions qiskit/circuit/library/grover_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from typing import List, Optional, Union
import numpy
from qiskit.circuit import QuantumCircuit, QuantumRegister, AncillaRegister
from qiskit.exceptions import QiskitError
from qiskit.quantum_info import Statevector, Operator, DensityMatrix
from .standard_gates import MCXGate

Expand Down Expand Up @@ -274,10 +275,10 @@ def _build(self):
circuit.global_phase = numpy.pi

self.add_register(*circuit.qregs)
if self._insert_barriers:
circuit_wrapped = circuit.to_instruction()
else:
try:
circuit_wrapped = circuit.to_gate()
except QiskitError:
circuit_wrapped = circuit.to_instruction()

self.compose(circuit_wrapped, qubits=self.qubits, inplace=True)

Expand Down
15 changes: 15 additions & 0 deletions test/python/circuit/library/test_grover_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,21 @@ def test_quantum_info_input(self):
grover_op, oracle=np.diag((-1) ** mark.data), zero_reflection=diffuse.data
)

def test_stateprep_contains_instruction(self):
"""Test wrapping works if the state preparation is not unitary."""
oracle = QuantumCircuit(1)
oracle.z(0)

instr = QuantumCircuit(1)
instr.s(0)
instr = instr.to_instruction()

stateprep = QuantumCircuit(1)
stateprep.append(instr, [0])

grover_op = GroverOperator(oracle, stateprep)
self.assertEqual(grover_op.num_qubits, 1)

def test_reflection_qubits(self):
"""Test setting idle qubits doesn't apply any operations on these qubits."""
oracle = QuantumCircuit(4)
Expand Down

0 comments on commit ecde497

Please sign in to comment.