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 generating subexperiments with LO's translated to a native gate set #517

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
4b819c7
Add support for generating subexperiments with LO's translated to a n…
caleb-johnson Mar 28, 2024
b89583c
Improve release note
caleb-johnson Mar 28, 2024
679fb73
Don't include basis gates in the equiv library
caleb-johnson Mar 28, 2024
43ed255
ruff
caleb-johnson Mar 28, 2024
7a5fe8a
Minor code clarity update
caleb-johnson Mar 28, 2024
aab23a2
Merge branch 'main' of github.com:Qiskit-Extensions/circuit-knitting-…
caleb-johnson Mar 30, 2024
c654f6a
Bug in RXGate. Add to roundtrip tests.
caleb-johnson Mar 30, 2024
73bccc4
Sample standard gate set 50% of time
caleb-johnson Mar 30, 2024
0472093
black
caleb-johnson Mar 30, 2024
fa0578c
theta name
caleb-johnson Mar 30, 2024
819cc90
Change name of kwarg.
caleb-johnson Mar 30, 2024
367e19c
black
caleb-johnson Mar 30, 2024
f286131
Rename how-to
caleb-johnson Mar 31, 2024
c881fbc
don't use defaultdict
caleb-johnson Apr 1, 2024
8d1745d
Update circuit_knitting/cutting/qpd/decompose.py
caleb-johnson Apr 1, 2024
ba8b744
fix tests
caleb-johnson Apr 1, 2024
4b796a9
Merge branch 'translate-sampled-gates' of github.com:Qiskit-Extension…
caleb-johnson Apr 1, 2024
b8351d1
Merge branch 'main' of github.com:Qiskit-Extensions/circuit-knitting-…
caleb-johnson May 14, 2024
6395205
Implement translation as a function. Use regular dictionaries under h…
caleb-johnson Jun 3, 2024
9cf2372
Rename equivalence --> translation
caleb-johnson Jun 3, 2024
f7e8cb1
style
caleb-johnson Jun 3, 2024
4448506
Clean up bugs
caleb-johnson Jun 3, 2024
4aac71f
Allow modern type hints for py38 users
caleb-johnson Jun 3, 2024
41b5881
Merge branch 'main' of github.com:Qiskit-Extensions/circuit-knitting-…
caleb-johnson Jun 3, 2024
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
9 changes: 8 additions & 1 deletion circuit_knitting/cutting/cutting_experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def generate_cutting_experiments(
circuits: QuantumCircuit | dict[Hashable, QuantumCircuit],
observables: PauliList | dict[Hashable, PauliList],
num_samples: int | float,
basis_gate_set: str | None = None,
) -> tuple[
list[QuantumCircuit] | dict[Hashable, list[QuantumCircuit]],
list[tuple[float, WeightType]],
Expand Down Expand Up @@ -64,6 +65,8 @@ def generate_cutting_experiments(
num_samples: The number of samples to draw from the quasi-probability distribution. If set
to infinity, the weights will be generated rigorously rather than by sampling from
the distribution.
basis_gate_set: A QPU architecture for which the sampled instructions should be
translated. Supported inputs are: {"heron", "eagle", None}
Returns:
A tuple containing the cutting experiments and their associated coefficients.
If the input circuits is a :class:`QuantumCircuit` instance, the output subexperiments
Expand Down Expand Up @@ -151,7 +154,11 @@ def generate_cutting_experiments(
for j, cog in enumerate(so.groups):
new_qc = _append_measurement_register(subcircuit, cog)
decompose_qpd_instructions(
new_qc, subcirc_qpd_gate_ids[label], map_ids_tmp, inplace=True
new_qc,
subcirc_qpd_gate_ids[label],
map_ids_tmp,
basis_gate_set=basis_gate_set,
inplace=True,
)
_append_measurement_circuit(new_qc, cog, inplace=True)
subexperiments_dict[label].append(new_qc)
Expand Down
2 changes: 2 additions & 0 deletions circuit_knitting/cutting/qpd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
TwoQubitQPDGate,
QPDMeasure,
)
from .translation import translate_qpd_gate

__all__ = [
"qpdbasis_from_instruction",
"generate_qpd_weights",
"decompose_qpd_instructions",
"translate_qpd_gate",
"QPDBasis",
"BaseQPDGate",
"TwoQubitQPDGate",
Expand Down
20 changes: 18 additions & 2 deletions circuit_knitting/cutting/qpd/decompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@
)

from .instructions import BaseQPDGate, TwoQubitQPDGate
from .translation import translate_qpd_gate


def decompose_qpd_instructions(
circuit: QuantumCircuit,
instruction_ids: Sequence[Sequence[int]],
map_ids: Sequence[int] | None = None,
*,
basis_gate_set: str | None = None,
inplace: bool = False,
) -> QuantumCircuit:
r"""
Expand All @@ -43,6 +45,9 @@ def decompose_qpd_instructions(
map_ids: Indices to a specific linear mapping to be applied to the decompositions
in the circuit. If no map IDs are provided, the circuit will be decomposed randomly
according to the decompositions' joint probability distribution.
basis_gate_set: A QPU architecture for which the sampled instructions should be
translated. Supported inputs are: {"heron", "eagle", None}
inplace: Whether to modify the input circuit directly

Returns:
Circuit which has had all its :class:`BaseQPDGate` instances decomposed into local operations.
Expand Down Expand Up @@ -76,7 +81,7 @@ def decompose_qpd_instructions(
circuit.data[gate_id].operation.basis_id = map_ids[i]

# Convert all instances of BaseQPDGate in the circuit to Qiskit instructions
_decompose_qpd_instructions(circuit, instruction_ids)
_decompose_qpd_instructions(circuit, instruction_ids, basis_gate_set=basis_gate_set)

return circuit

Expand Down Expand Up @@ -170,6 +175,7 @@ def _decompose_qpd_instructions(
circuit: QuantumCircuit,
instruction_ids: Sequence[Sequence[int]],
inplace: bool = True,
basis_gate_set: str | None = None,
) -> QuantumCircuit:
"""Decompose all BaseQPDGate instances, ignoring QPDMeasure()."""
if not inplace:
Expand Down Expand Up @@ -198,6 +204,10 @@ def _decompose_qpd_instructions(
data_id_offset += 1
circuit.data.insert(i + data_id_offset, inst2)

# Get equivalence library
if basis_gate_set is not None:
basis_gate_set = basis_gate_set.lower()

# Decompose all the QPDGates (should all be single qubit now) into Qiskit operations
new_instruction_ids = []
for i, inst in enumerate(circuit.data):
Expand All @@ -214,7 +224,13 @@ def _decompose_qpd_instructions(
for data in inst.operation.definition.data:
# Can ignore clbits here, as QPDGates don't use clbits directly
assert data.clbits == ()
tmp_data.append(CircuitInstruction(data.operation, qubits=[qubits[0]]))
if basis_gate_set is None or data.operation.name in {"qpd_measure"}:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Anything that comes out of QPDBasis can be translated except our measurement objects. Those never need to be translated

tmp_data.append(CircuitInstruction(data.operation, qubits=[qubits[0]]))
else:
equiv_circ = translate_qpd_gate(data.operation, basis_gate_set)
for d in equiv_circ.data:
tmp_data.append(CircuitInstruction(d.operation, qubits=[qubits[0]]))

# Replace QPDGate with local operations
if tmp_data:
# Overwrite the QPDGate with first instruction
Expand Down
227 changes: 227 additions & 0 deletions circuit_knitting/cutting/qpd/translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
# This code is a Qiskit project.

# (C) Copyright IBM 2024.

# 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.

"""
Equivalence utilities.

.. currentmodule:: circuit_knitting.cutting.qpd.equivalence

.. autosummary::
:toctree: ../stubs/

"""
from __future__ import annotations

from collections.abc import Callable

import numpy as np
from qiskit.circuit import QuantumCircuit, QuantumRegister, Gate
from qiskit.circuit.library.standard_gates import (
RZGate,
XGate,
YGate,
ZGate,
HGate,
SGate,
SdgGate,
SXGate,
SXdgGate,
TGate,
TdgGate,
RXGate,
RYGate,
PhaseGate,
)


_equivalence_from_gate_funcs: dict[str, Callable[[Gate], QuantumCircuit]] = {}


def _register_gate(*args):
def g(f):
for name in args:
_equivalence_from_gate_funcs[name] = f
return f

return g


def translate_qpd_gate(gate: Gate, basis_gate_set: str, /) -> QuantumCircuit:
"""
Translate a ``gate`` into a given basis gate set.

This function is designed to handle only the gates to which a :class:`.QPDBasis` can
decompose; therefore, not every Qiskit gate is supported by this function.

Args:
gate: The gate to translate

Returns:
A :class:`qiskit.QuantumCircuit` implementing the gate in the given basis gate set.

Raises:
ValueError: Unsupported basis gate set
ValueError: Unsupported gate
"""
# We otherwise ignore this arg for now since our only two equivalences are equivalent :)
if basis_gate_set not in {"heron", "eagle"}:
raise ValueError(f"Unknown basis gate set: {basis_gate_set}")
try:
f = _equivalence_from_gate_funcs[gate.name]
except KeyError as exc:
raise ValueError(f"Cannot translate gate: {gate.name}") from exc
else:
return f(gate)


# XGate
@_register_gate("x")
def _(gate: XGate):
q = QuantumRegister(1, "q")
def_x = QuantumCircuit(q)
def_x.append(gate, [0], [])
return def_x


# SXGate
@_register_gate("sx")
def _(gate: SXGate):
q = QuantumRegister(1, "q")
def_sx = QuantumCircuit(q)
def_sx.append(gate, [0], [])
return def_sx


# RZGate
@_register_gate("rz")
def _(gate: RZGate):
q = QuantumRegister(1, "q")
def_rz = QuantumCircuit(q)
def_rz.append(gate, [0], [])
return def_rz


# YGate
@_register_gate("y")
def _(_: YGate):
q = QuantumRegister(1, "q")
def_y = QuantumCircuit(q)
for inst in [RZGate(np.pi), XGate()]:
def_y.append(inst, [0], [])
return def_y


# ZGate
@_register_gate("z")
def _(_: ZGate):
q = QuantumRegister(1, "q")
def_z = QuantumCircuit(q)
def_z.append(RZGate(np.pi), [0], [])
return def_z


# HGate
@_register_gate("h")
def _(_: HGate):
q = QuantumRegister(1, "q")
def_h = QuantumCircuit(q)
for inst in [RZGate(np.pi / 2), SXGate(), RZGate(np.pi / 2)]:
def_h.append(inst, [0], [])
return def_h


# SGate
@_register_gate("s")
def _(_: SGate):
q = QuantumRegister(1, "q")
def_s = QuantumCircuit(q)
def_s.append(RZGate(np.pi / 2), [0], [])
return def_s


# SdgGate
@_register_gate("sdg")
def _(_: SdgGate):
q = QuantumRegister(1, "q")
def_sdg = QuantumCircuit(q)
def_sdg.append(RZGate(-np.pi / 2), [0], [])
return def_sdg


# SXdgGate
@_register_gate("sxdg")
def _(_: SXdgGate):
q = QuantumRegister(1, "q")
def_sxdg = QuantumCircuit(q)
for inst in [
RZGate(np.pi / 2),
RZGate(np.pi / 2),
SXGate(),
RZGate(np.pi / 2),
RZGate(np.pi / 2),
]:
def_sxdg.append(inst, [0], [])
return def_sxdg


# TGate
@_register_gate("t")
def _(_: TGate):
q = QuantumRegister(1, "q")
def_t = QuantumCircuit(q)
def_t.append(RZGate(np.pi / 4), [0], [])
return def_t


# TdgGate
@_register_gate("tdg")
def _(_: TdgGate):
q = QuantumRegister(1, "q")
def_tdg = QuantumCircuit(q)
def_tdg.append(RZGate(-np.pi / 4), [0], [])
return def_tdg


# RXGate
@_register_gate("rx")
def _(gate: RXGate):
q = QuantumRegister(1, "q")
def_rx = QuantumCircuit(q)
param = gate.params[0]
for inst in [
RZGate(np.pi / 2),
SXGate(),
RZGate(param + np.pi),
SXGate(),
RZGate(5 * np.pi / 2),
]:
def_rx.append(inst, [0], [])
return def_rx


# RYGate
@_register_gate("ry")
def _(gate: RYGate):
q = QuantumRegister(1, "q")
def_ry = QuantumCircuit(q)
param = gate.params[0]
for inst in [SXGate(), RZGate(param + np.pi), SXGate(), RZGate(3 * np.pi)]:
def_ry.append(inst, [0], [])


# PhaseGate
@_register_gate("p")
def _(gate: PhaseGate):
q = QuantumRegister(1, "q")
def_p = QuantumCircuit(q)
param = gate.params[0]
def_p.append(RZGate(param), [0], [])
return def_p
Loading
Loading