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

Add support for SingleQubitQPDGate to be passed through the transpiler #303

Merged
merged 2 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions circuit_knitting/cutting/qpd/instructions/qpd_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ def _define(self) -> None:
qc.append(CircuitInstruction(op, [qc.qubits[0]], []))
self.definition = qc

@property
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please just quickly explain this mechanism here? Not familiar with _directive

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is something that @mtreinish told me about. It tells the transpiler to treat it as something special that ought to be passed through most passes without being touched, and is used for Barriers and a few other things.

See this brief description, which itself links to Qiskit/qiskit#5811

def _directive(self):
"""``True`` if the ``basis_id`` is unassigned, which implies this instruction cannot be decomposed."""
return self.basis_id is None

def __eq__(self, other):
"""Check equivalence for SingleQubitQPDGate class."""
return super().__eq__(other) and self.qubit_id == other.qubit_id
50 changes: 50 additions & 0 deletions test/cutting/test_cutting_workflows.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# This code is a Qiskit project.

# (C) Copyright IBM 2023.

# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""Tests of various cutting workflows, particularly with regard to flexibility in transpilation."""

from copy import deepcopy

from qiskit.circuit.library import EfficientSU2, CXGate
from qiskit.quantum_info import PauliList
from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager
from qiskit.providers.fake_provider import FakeLagosV2

from circuit_knitting.cutting.qpd.instructions import SingleQubitQPDGate
from circuit_knitting.cutting.qpd import QPDBasis
from circuit_knitting.cutting import partition_problem


def test_transpile_before_realizing_basis_id():
"""Test a workflow where a :class:`.SingleQubitQPDGate` is passed through the transpiler."""
circuit = EfficientSU2(4, entanglement="linear", reps=2).decompose()
circuit.assign_parameters([0.8] * len(circuit.parameters), inplace=True)
observables = PauliList(["ZZII"])
subcircuits, bases, subobservables = partition_problem(
circuit=circuit, partition_labels="AABB", observables=observables
)

# Create a fake backend, and modify the target gate set so it thinks a
# SingleQubitQPDGate is allowed.
backend = FakeLagosV2()
backend = deepcopy(backend)
sample_qpd_instruction = SingleQubitQPDGate(QPDBasis.from_gate(CXGate()), 1)
backend.target.add_instruction(
sample_qpd_instruction,
{(i,): None for i in range(backend.target.num_qubits)},
)
pass_manager = generate_preset_pass_manager(3, backend)
garrison marked this conversation as resolved.
Show resolved Hide resolved

# Pass each subcircuit through the pass manager.
subcircuits = {
label: pass_manager.run(subcircuits["A"])
for label, circuit in subcircuits.items()
}