GroverOperator

class GroverOperator(oracle, state_preparation=None, zero_reflection=None, reflection_qubits=None, insert_barriers=False, mcx_mode='noancilla', name='Q')[source]

The Grover operator.

Grover’s search algorithm [1, 2] consists of repeated applications of the so-called Grover operator used to amplify the amplitudes of the desired output states. This operator, \(\mathcal{Q}\), consists of the phase oracle, \(\mathcal{S}_f\), zero phase-shift or zero reflection, \(\mathcal{S}_0\), and an input state preparation \(\mathcal{A}\):

\[\mathcal{Q} = \mathcal{A} \mathcal{S}_0 \mathcal{A}^\dagger \mathcal{S}_f\]

In the standard Grover search we have \(\mathcal{A} = H^{\otimes n}\):

\[\mathcal{Q} = H^{\otimes n} \mathcal{S}_0 H^{\otimes n} \mathcal{S}_f = D \mathcal{S_f}\]

The operation \(D = H^{\otimes n} \mathcal{S}_0 H^{\otimes n}\) is also referred to as diffusion operator. In this formulation we can see that Grover’s operator consists of two steps: first, the phase oracle multiplies the good states by -1 (with \(\mathcal{S}_f\)) and then the whole state is reflected around the mean (with \(D\)).

This class allows setting a different state preparation, as in quantum amplitude amplification (a generalization of Grover’s algorithm), \(\mathcal{A}\) might not be a layer of Hardamard gates [3].

The action of the phase oracle \(\mathcal{S}_f\) is defined as

\[\mathcal{S}_f: |x\rangle \mapsto (-1)^{f(x)}|x\rangle\]

where \(f(x) = 1\) if \(x\) is a good state and 0 otherwise. To highlight the fact that this oracle flips the phase of the good states and does not flip the state of a result qubit, we call \(\mathcal{S}_f\) a phase oracle.

Note that you can easily construct a phase oracle from a bitflip oracle by sandwiching the controlled X gate on the result qubit by a X and H gate. For instance

Bitflip oracle     Phaseflip oracle
q_0: ──■──         q_0: ────────────■────────────
     ┌─┴─┐              ┌───┐┌───┐┌─┴─┐┌───┐┌───┐
out: ┤ X ├         out: ┤ X ├┤ H ├┤ X ├┤ H ├┤ X ├
     └───┘              └───┘└───┘└───┘└───┘└───┘

There is some flexibility in defining the oracle and \(\mathcal{A}\) operator. Before the Grover operator is applied in Grover’s algorithm, the qubits are first prepared with one application of the \(\mathcal{A}\) operator (or Hadamard gates in the standard formulation). Thus, we always have operation of the form \(\mathcal{A} \mathcal{S}_f \mathcal{A}^\dagger\). Therefore it is possible to move bitflip logic into \(\mathcal{A}\) and leaving the oracle only to do phaseflips via Z gates based on the bitflips. One possible use-case for this are oracles that do not uncompute the state qubits.

The zero reflection \(\mathcal{S}_0\) is usually defined as

\[\mathcal{S}_0 = 2 |0\rangle^{\otimes n} \langle 0|^{\otimes n} - \mathbb{I}_n\]

where \(\mathbb{I}_n\) is the identity on \(n\) qubits. By default, this class implements the negative version \(2 |0\rangle^{\otimes n} \langle 0|^{\otimes n} - \mathbb{I}_n\), since this can simply be implemented with a multi-controlled Z sandwiched by X gates on the target qubit and the introduced global phase does not matter for Grover’s algorithm.

Examples

>>> from qiskit.circuit import QuantumCircuit
>>> from qiskit.circuit.library import GroverOperator
>>> oracle = QuantumCircuit(2)
>>> oracle.z(0)  # good state = first qubit is |1>
>>> grover_op = GroverOperator(oracle, insert_barriers=True)
>>> grover_op.draw()
         ┌───┐ ░ ┌───┐ ░ ┌───┐          ┌───┐      ░ ┌───┐
state_0: ┤ Z ├─░─┤ H ├─░─┤ X ├───────■──┤ X ├──────░─┤ H ├
         └───┘ ░ ├───┤ ░ ├───┤┌───┐┌─┴─┐├───┤┌───┐ ░ ├───┤
state_1: ──────░─┤ H ├─░─┤ X ├┤ H ├┤ X ├┤ H ├┤ X ├─░─┤ H ├
               ░ └───┘ ░ └───┘└───┘└───┘└───┘└───┘ ░ └───┘
>>> oracle = QuantumCircuit(1)
>>> oracle.z(0)  # the qubit state |1> is the good state
>>> state_preparation = QuantumCircuit(1)
>>> state_preparation.ry(0.2, 0)  # non-uniform state preparation
>>> grover_op = GroverOperator(oracle, state_preparation)
>>> grover_op.draw()
         ┌───┐┌──────────┐┌───┐┌───┐┌───┐┌─────────┐
state_0: ┤ Z ├┤ RY(-0.2) ├┤ X ├┤ Z ├┤ X ├┤ RY(0.2) ├
         └───┘└──────────┘└───┘└───┘└───┘└─────────┘
>>> oracle = QuantumCircuit(4)
>>> oracle.z(3)
>>> reflection_qubits = [0, 3]
>>> state_preparation = QuantumCircuit(4)
>>> state_preparation.cry(0.1, 0, 3)
>>> state_preparation.ry(0.5, 3)
>>> grover_op = GroverOperator(oracle, state_preparation,
... reflection_qubits=reflection_qubits)
>>> grover_op.draw()
                                      ┌───┐          ┌───┐
state_0: ──────────────────────■──────┤ X ├───────■──┤ X ├──────────■────────────────
                               │      └───┘       │  └───┘          │
state_1: ──────────────────────┼──────────────────┼─────────────────┼────────────────
                               │                  │                 │
state_2: ──────────────────────┼──────────────────┼─────────────────┼────────────────
         ┌───┐┌──────────┐┌────┴─────┐┌───┐┌───┐┌─┴─┐┌───┐┌───┐┌────┴────┐┌─────────┐
state_3: ┤ Z ├┤ RY(-0.5) ├┤ RY(-0.1) ├┤ X ├┤ H ├┤ X ├┤ H ├┤ X ├┤ RY(0.1) ├┤ RY(0.5) ├
         └───┘└──────────┘└──────────┘└───┘└───┘└───┘└───┘└───┘└─────────┘└─────────┘
>>> mark_state = Statevector.from_label('011')
>>> diffuse_operator = 2 * DensityMatrix.from_label('000') - Operator.from_label('III')
>>> grover_op = GroverOperator(oracle=mark_state, zero_reflection=diffuse_operator)
>>> grover_op.draw(fold=70)
         ┌─────────────────┐      ┌───┐                          »
state_0: ┤0                ├──────┤ H ├──────────────────────────»
         │                 │┌─────┴───┴─────┐     ┌───┐          »
state_1: ┤1 UCRZ(0,pi,0,0) ├┤0              ├─────┤ H ├──────────»
         │                 ││  UCRZ(pi/2,0) │┌────┴───┴────┐┌───┐»
state_2: ┤2                ├┤1              ├┤ UCRZ(-pi/4) ├┤ H ├»
         └─────────────────┘└───────────────┘└─────────────┘└───┘»
«         ┌─────────────────┐      ┌───┐
«state_0: ┤0                ├──────┤ H ├─────────────────────────
«         │                 │┌─────┴───┴─────┐    ┌───┐
«state_1: ┤1 UCRZ(pi,0,0,0) ├┤0              ├────┤ H ├──────────
«         │                 ││  UCRZ(pi/2,0) │┌───┴───┴────┐┌───┐
«state_2: ┤2                ├┤1              ├┤ UCRZ(pi/4) ├┤ H ├
«         └─────────────────┘└───────────────┘└────────────┘└───┘

References

[1]: L. K. Grover (1996), A fast quantum mechanical algorithm for database search,

arXiv:quant-ph/9605043.

[2]: I. Chuang & M. Nielsen, Quantum Computation and Quantum Information,

Cambridge: Cambridge University Press, 2000. Chapter 6.1.2.

[3]: Brassard, G., Hoyer, P., Mosca, M., & Tapp, A. (2000).

Quantum Amplitude Amplification and Estimation. arXiv:quant-ph/0005055.

Parameters
  • oracle (Union[QuantumCircuit, Statevector]) – The phase oracle implementing a reflection about the bad state. Note that this is not a bitflip oracle, see the docstring for more information.

  • state_preparation (Optional[QuantumCircuit]) – The operator preparing the good and bad state. For Grover’s algorithm, this is a n-qubit Hadamard gate and for amplitude amplification or estimation the operator \(\mathcal{A}\).

  • zero_reflection (Union[QuantumCircuit, DensityMatrix, Operator, None]) – The reflection about the zero state, \(\mathcal{S}_0\).

  • reflection_qubits (Optional[List[int]]) – Qubits on which the zero reflection acts on.

  • insert_barriers (bool) – Whether barriers should be inserted between the reflections and A.

  • mcx_mode (str) – The mode to use for building the default zero reflection.

  • name (str) – The name of the circuit.

Attributes

GroverOperator.ancillas

Returns a list of ancilla bits in the order that the registers were added.

GroverOperator.calibrations

Return calibration dictionary.

GroverOperator.clbits

Returns a list of classical bits in the order that the registers were added.

GroverOperator.data

Return the circuit data (instructions and context).

GroverOperator.extension_lib

GroverOperator.global_phase

Return the global phase of the circuit in radians.

GroverOperator.header

GroverOperator.instances

GroverOperator.num_ancillas

Return the number of ancilla qubits.

GroverOperator.num_clbits

Return number of classical bits.

GroverOperator.num_parameters

Convenience function to get the number of parameter objects in the circuit.

GroverOperator.num_qubits

Return number of qubits.

GroverOperator.oracle

The oracle implementing a reflection about the bad state.

GroverOperator.parameters

Convenience function to get the parameters defined in the parameter table.

GroverOperator.prefix

GroverOperator.qubits

Returns a list of quantum bits in the order that the registers were added.

GroverOperator.reflection_qubits

Reflection qubits, on which S0 is applied (if S0 is not user-specified).

GroverOperator.state_preparation

The subcircuit implementing the A operator or Hadamards.

GroverOperator.zero_reflection

The subcircuit implementing the reflection about 0.

Methods

GroverOperator.__getitem__(item)

Return indexed operation.

GroverOperator.__len__()

Return number of operations in circuit.

GroverOperator.add_calibration(gate, qubits, …)

Register a low-level, custom pulse definition for the given gate.

GroverOperator.add_register(*regs)

Add registers.

GroverOperator.append(instruction[, qargs, …])

Append one or more instructions to the end of the circuit, modifying the circuit in place.

GroverOperator.assign_parameters(param_dict)

Assign parameters to new parameters or values.

GroverOperator.barrier(*qargs)

Apply Barrier.

GroverOperator.bind_parameters(value_dict)

Assign numeric parameters to values yielding a new circuit.

GroverOperator.cast(value, _type)

Best effort to cast value to type.

GroverOperator.cbit_argument_conversion(…)

Converts several classical bit representations (such as indexes, range, etc.) into a list of classical bits.

GroverOperator.ccx(control_qubit1, …)

Apply CCXGate.

GroverOperator.ch(control_qubit, target_qubit)

Apply CHGate.

GroverOperator.cls_instances()

Return the current number of instances of this class, useful for auto naming.

GroverOperator.cls_prefix()

Return the prefix to use for auto naming.

GroverOperator.cnot(control_qubit, target_qubit)

Apply CXGate.

GroverOperator.combine(rhs)

Append rhs to self if self contains compatible registers.

GroverOperator.compose(other[, qubits, …])

Compose circuit with other circuit or instruction, optionally permuting wires.

GroverOperator.control([num_ctrl_qubits, …])

Control this circuit on num_ctrl_qubits qubits.

GroverOperator.copy([name])

Copy the circuit.

GroverOperator.count_ops()

Count each operation kind in the circuit.

GroverOperator.cp(theta, control_qubit, …)

Apply CPhaseGate.

GroverOperator.crx(theta, control_qubit, …)

Apply CRXGate.

GroverOperator.cry(theta, control_qubit, …)

Apply CRYGate.

GroverOperator.crz(theta, control_qubit, …)

Apply CRZGate.

GroverOperator.cswap(control_qubit, …[, …])

Apply CSwapGate.

GroverOperator.csx(control_qubit, target_qubit)

Apply CSXGate.

GroverOperator.cu(theta, phi, lam, gamma, …)

Apply CUGate.

GroverOperator.cu1(theta, control_qubit, …)

Apply CU1Gate.

GroverOperator.cu3(theta, phi, lam, …[, …])

Apply CU3Gate.

GroverOperator.cx(control_qubit, target_qubit)

Apply CXGate.

GroverOperator.cy(control_qubit, target_qubit)

Apply CYGate.

GroverOperator.cz(control_qubit, target_qubit)

Apply CZGate.

GroverOperator.dcx(qubit1, qubit2)

Apply DCXGate.

GroverOperator.decompose()

Call a decomposition pass on this circuit, to decompose one level (shallow decompose).

GroverOperator.delay(duration[, qarg, unit])

Apply Delay.

GroverOperator.depth()

Return circuit depth (i.e., length of critical path).

GroverOperator.diag_gate(diag, qubit)

Deprecated version of QuantumCircuit.diagonal.

GroverOperator.diagonal(diag, qubit)

Attach a diagonal gate to a circuit.

GroverOperator.draw([output, scale, …])

Draw the quantum circuit.

GroverOperator.extend(rhs)

Append QuantumCircuit to the right hand side if it contains compatible registers.

GroverOperator.fredkin(control_qubit, …)

Apply CSwapGate.

GroverOperator.from_qasm_file(path)

Take in a QASM file and generate a QuantumCircuit object.

GroverOperator.from_qasm_str(qasm_str)

Take in a QASM string and generate a QuantumCircuit object.

GroverOperator.h(qubit)

Apply HGate.

GroverOperator.hamiltonian(operator, time, …)

Apply hamiltonian evolution to to qubits.

GroverOperator.has_register(register)

Test if this circuit has the register r.

GroverOperator.i(qubit)

Apply IGate.

GroverOperator.id(qubit)

Apply IGate.

GroverOperator.initialize(params, qubits)

Apply initialize to circuit.

GroverOperator.inverse()

Invert (take adjoint of) this circuit.

GroverOperator.iso(isometry, q_input, …[, …])

Attach an arbitrary isometry from m to n qubits to a circuit.

GroverOperator.isometry(isometry, q_input, …)

Attach an arbitrary isometry from m to n qubits to a circuit.

GroverOperator.iswap(qubit1, qubit2)

Apply iSwapGate.

GroverOperator.mcmt(gate, control_qubits, …)

Apply a multi-control, multi-target using a generic gate.

GroverOperator.mcp(lam, control_qubits, …)

Apply MCPhaseGate.

GroverOperator.mcrx(theta, q_controls, q_target)

Apply Multiple-Controlled X rotation gate

GroverOperator.mcry(theta, q_controls, …)

Apply Multiple-Controlled Y rotation gate

GroverOperator.mcrz(lam, q_controls, q_target)

Apply Multiple-Controlled Z rotation gate

GroverOperator.mct(control_qubits, target_qubit)

Apply MCXGate.

GroverOperator.mcu1(lam, control_qubits, …)

Apply MCU1Gate.

GroverOperator.mcx(control_qubits, target_qubit)

Apply MCXGate.

GroverOperator.measure(qubit, cbit)

Measure quantum bit into classical bit (tuples).

GroverOperator.measure_active([inplace])

Adds measurement to all non-idle qubits.

GroverOperator.measure_all([inplace])

Adds measurement to all qubits.

GroverOperator.mirror()

DEPRECATED: use circuit.reverse_ops().

GroverOperator.ms(theta, qubits)

Apply MSGate.

GroverOperator.num_connected_components([…])

How many non-entangled subcircuits can the circuit be factored to.

GroverOperator.num_nonlocal_gates()

Return number of non-local gates (i.e.

GroverOperator.num_tensor_factors()

Computes the number of tensor factors in the unitary (quantum) part of the circuit only.

GroverOperator.num_unitary_factors()

Computes the number of tensor factors in the unitary (quantum) part of the circuit only.

GroverOperator.p(theta, qubit)

Apply PhaseGate.

GroverOperator.power(power[, matrix_power])

Raise this circuit to the power of power.

GroverOperator.qasm([formatted, filename])

Return OpenQASM string.

GroverOperator.qbit_argument_conversion(…)

Converts several qubit representations (such as indexes, range, etc.) into a list of qubits.

GroverOperator.qubit_duration(*qubits)

Return the duration between the start and stop time of the first and last instructions, excluding delays, over the supplied qubits.

GroverOperator.qubit_start_time(*qubits)

Return the start time of the first instruction, excluding delays, over the supplied qubits.

GroverOperator.qubit_stop_time(*qubits)

Return the stop time of the last instruction, excluding delays, over the supplied qubits.

GroverOperator.r(theta, phi, qubit)

Apply RGate.

GroverOperator.rcccx(control_qubit1, …)

Apply RC3XGate.

GroverOperator.rccx(control_qubit1, …)

Apply RCCXGate.

GroverOperator.remove_final_measurements([…])

Removes final measurement on all qubits if they are present.

GroverOperator.repeat(reps)

Repeat this circuit reps times.

GroverOperator.reset(qubit)

Reset q.

GroverOperator.reverse_bits()

Return a circuit with the opposite order of wires.

GroverOperator.reverse_ops()

Reverse the circuit by reversing the order of instructions.

GroverOperator.rx(theta, qubit[, label])

Apply RXGate.

GroverOperator.rxx(theta, qubit1, qubit2)

Apply RXXGate.

GroverOperator.ry(theta, qubit[, label])

Apply RYGate.

GroverOperator.ryy(theta, qubit1, qubit2)

Apply RYYGate.

GroverOperator.rz(phi, qubit)

Apply RZGate.

GroverOperator.rzx(theta, qubit1, qubit2)

Apply RZXGate.

GroverOperator.rzz(theta, qubit1, qubit2)

Apply RZZGate.

GroverOperator.s(qubit)

Apply SGate.

GroverOperator.sdg(qubit)

Apply SdgGate.

GroverOperator.size()

Returns total number of gate operations in circuit.

GroverOperator.snapshot(label[, …])

Take a statevector snapshot of the internal simulator representation.

GroverOperator.snapshot_density_matrix(label)

Take a density matrix snapshot of simulator state.

GroverOperator.snapshot_expectation_value(…)

Take a snapshot of expectation value <O> of an Operator.

GroverOperator.snapshot_probabilities(label, …)

Take a probability snapshot of the simulator state.

GroverOperator.snapshot_stabilizer(label)

Take a stabilizer snapshot of the simulator state.

GroverOperator.snapshot_statevector(label)

Take a statevector snapshot of the simulator state.

GroverOperator.squ(unitary_matrix, qubit[, …])

Decompose an arbitrary 2*2 unitary into three rotation gates.

GroverOperator.swap(qubit1, qubit2)

Apply SwapGate.

GroverOperator.sx(qubit)

Apply SXGate.

GroverOperator.sxdg(qubit)

Apply SXdgGate.

GroverOperator.t(qubit)

Apply TGate.

GroverOperator.tdg(qubit)

Apply TdgGate.

GroverOperator.to_gate([parameter_map, label])

Create a Gate out of this circuit.

GroverOperator.to_instruction([parameter_map])

Create an Instruction out of this circuit.

GroverOperator.toffoli(control_qubit1, …)

Apply CCXGate.

GroverOperator.u(theta, phi, lam, qubit)

Apply UGate.

GroverOperator.u1(theta, qubit)

Apply U1Gate.

GroverOperator.u2(phi, lam, qubit)

Apply U2Gate.

GroverOperator.u3(theta, phi, lam, qubit)

Apply U3Gate.

GroverOperator.uc(gate_list, q_controls, …)

Attach a uniformly controlled gates (also called multiplexed gates) to a circuit.

GroverOperator.ucrx(angle_list, q_controls, …)

Attach a uniformly controlled (also called multiplexed) Rx rotation gate to a circuit.

GroverOperator.ucry(angle_list, q_controls, …)

Attach a uniformly controlled (also called multiplexed) Ry rotation gate to a circuit.

GroverOperator.ucrz(angle_list, q_controls, …)

Attach a uniformly controlled (also called multiplexed gates) Rz rotation gate to a circuit.

GroverOperator.unitary(obj, qubits[, label])

Apply unitary gate to q.

GroverOperator.width()

Return number of qubits plus clbits in circuit.

GroverOperator.x(qubit[, label])

Apply XGate.

GroverOperator.y(qubit)

Apply YGate.

GroverOperator.z(qubit)

Apply ZGate.

GroverOperator.__getitem__(item)

Return indexed operation.

GroverOperator.__len__()

Return number of operations in circuit.