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 7 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 @@ -41,6 +41,7 @@ def generate_cutting_experiments(
circuits: QuantumCircuit | dict[Hashable, QuantumCircuit],
observables: PauliList | dict[Hashable, PauliList],
num_samples: int | float,
translate_to_qpu: str | None = None,
) -> tuple[
list[QuantumCircuit] | dict[Hashable, list[QuantumCircuit]],
list[tuple[float, WeightType]],
Expand Down Expand Up @@ -74,6 +75,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.
translate_to_qpu: 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 @@ -161,7 +164,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,
translate_to_qpu=translate_to_qpu,
inplace=True,
)
_append_measurement_circuit(new_qc, cog, inplace=True)
subexperiments_dict[label].append(new_qc)
Expand Down
35 changes: 33 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 ...utils.equivalence import equivalence_libraries


def decompose_qpd_instructions(
circuit: QuantumCircuit,
instruction_ids: Sequence[Sequence[int]],
map_ids: Sequence[int] | None = None,
*,
translate_to_qpu: 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.
translate_to_qpu: 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,9 @@ 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, translate_to_qpu=translate_to_qpu
)

return circuit

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

# Get equivalence library
if translate_to_qpu is not None:
translate_to_qpu = translate_to_qpu.lower()
else:
translate_to_qpu = "standard"
caleb-johnson marked this conversation as resolved.
Show resolved Hide resolved
equivalence = equivalence_libraries[translate_to_qpu]

# 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 +229,23 @@ 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 equivalence is None:
tmp_data.append(CircuitInstruction(data.operation, qubits=[qubits[0]]))
else:
equiv_entry = equivalence.get_entry(data.operation)
# CKT SELs currently only provide at most one translation
assert len(equiv_entry) <= 1
if equiv_entry == []:
tmp_data.append(
CircuitInstruction(data.operation, qubits=[qubits[0]])
)
else:
new_insts = equiv_entry[0]
for d in new_insts.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
6 changes: 6 additions & 0 deletions circuit_knitting/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,10 @@
===================================================================

.. automodule:: circuit_knitting.utils.transpiler_passes

===================================================================
Gate equivalence rules (:mod:`circuit_knitting.utils.equivalence`)
===================================================================

.. automodule:: circuit_knitting.utils.equivalence
"""
132 changes: 132 additions & 0 deletions circuit_knitting/utils/equivalence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
# 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.utils.equivalence

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

"""
from collections import defaultdict

import numpy as np
from qiskit.circuit import (
EquivalenceLibrary,
caleb-johnson marked this conversation as resolved.
Show resolved Hide resolved
QuantumCircuit,
QuantumRegister,
Parameter,
)
from qiskit.circuit.library.standard_gates import (
RZGate,
XGate,
YGate,
ZGate,
HGate,
SGate,
SdgGate,
SXGate,
SXdgGate,
TGate,
TdgGate,
RXGate,
RYGate,
PhaseGate,
)

_eagle_sel = HeronEquivalenceLibrary = EagleEquivalenceLibrary = EquivalenceLibrary()
equivalence_libraries = defaultdict(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Right now, if a user passes an unsupported basis, everything will run fine and their subexperiments will be in the standard gate set. Maybe we'd prefer to error if they pass in a QPU architecture we don't support or doesn't exist?

Copy link
Member

Choose a reason for hiding this comment

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

if a user passes an unsupported basis

By that, do you mean that they have a QPDBasis that contains some gate(s) that are not supported by this equivalence library?

everything will run fine and their subexperiments will be in the standard gate set

What do you mean by "everything will run fine"? What will happen to the unsupported gates?

Copy link
Collaborator Author

@caleb-johnson caleb-johnson Apr 1, 2024

Choose a reason for hiding this comment

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

If a user passes basis_gate_set="nonsense", their gates will come out in the standard gate set defined in decompositions.py. In other words, it's a no-op

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

By that, do you mean that they have a QPDBasis that contains some gate(s) that are not supported by this equivalence library?

I mean they pass in a string that doesn't describe a supported QPU architecture. A string not in {"heron", "eagle"}

lambda: None, {"heron": HeronEquivalenceLibrary, "eagle": EagleEquivalenceLibrary}
)

######################################################################

# YGate
q = QuantumRegister(1, "q")
def_y = QuantumCircuit(q)
for inst in [RZGate(np.pi), XGate()]:
def_y.append(inst, [0], [])
_eagle_sel.add_equivalence(YGate(), def_y)

# ZGate
q = QuantumRegister(1, "q")
def_z = QuantumCircuit(q)
def_z.append(RZGate(np.pi), [0], [])
_eagle_sel.add_equivalence(ZGate(), def_z)

# 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], [])
_eagle_sel.add_equivalence(HGate(), def_h)

# SGate
q = QuantumRegister(1, "q")
def_s = QuantumCircuit(q)
def_s.append(RZGate(np.pi / 2), [0], [])
_eagle_sel.add_equivalence(SGate(), def_s)

# SdgGate
q = QuantumRegister(1, "q")
def_sdg = QuantumCircuit(q)
def_sdg.append(RZGate(-np.pi / 2), [0], [])
_eagle_sel.add_equivalence(SdgGate(), def_sdg)

# SXdgGate
q = QuantumRegister(1, "q")
def_sxdg = QuantumCircuit(q)
for inst in [
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Another suspicious one. Should be able to do a single pi rotation on either side of sx

RZGate(np.pi / 2),
RZGate(np.pi / 2),
SXGate(),
RZGate(np.pi / 2),
RZGate(np.pi / 2),
]:
def_sxdg.append(inst, [0], [])
_eagle_sel.add_equivalence(SXdgGate(), def_sxdg)

# TGate
q = QuantumRegister(1, "q")
def_t = QuantumCircuit(q)
def_t.append(RZGate(np.pi / 4), [0], [])
_eagle_sel.add_equivalence(TGate(), def_t)

# TdgGate
q = QuantumRegister(1, "q")
def_tdg = QuantumCircuit(q)
def_tdg.append(RZGate(-np.pi / 4), [0], [])
_eagle_sel.add_equivalence(TdgGate(), def_tdg)

# RXGate
q = QuantumRegister(1, "q")
def_rx = QuantumCircuit(q)
theta = Parameter("theta")
for inst in [RZGate(np.pi / 2), SXGate(), RZGate(theta + np.pi), SXGate(), RZGate(5 * np.pi / 2)]:
def_rx.append(inst, [0], [])
_eagle_sel.add_equivalence(RXGate(theta), def_rx)

# RYGate
q = QuantumRegister(1, "q")
def_ry = QuantumCircuit(q)
theta = Parameter("theta")
for inst in [SXGate(), RZGate(theta + np.pi), SXGate(), RZGate(3 * np.pi)]:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Should be able to rotate by pi at the end here

Copy link
Member

Choose a reason for hiding this comment

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

I suspect that the reason there are factors of greater than 2pi in the equivalence library is because a rotation of 2pi doesn't actually bring the wavefunction to its original state; instead, e.g., for RZGate, it leads to the wavefunction picking up a global phase of -1. The rotation must be by an angle of 4pi to bring its state completely to where it began (see also: the mathematics of spinors). Even though a rotation by 2pi leads to a global phase of -1, this will not result in any difference in the state that is actually physically observable, but nonetheless Qiskit carefully keeps track of global phases, and I believe this is one instance of where that leads to some rotation angles that seem a bit atypical.

Here's a quick sanity check (in julia) given the RZGate definition of a rotation by 2pi:

In [1]: RZ(λ) = [exp(-im * λ / 2) 0; 0 exp(im * λ / 2)]
Out[1]: RZ (generic function with 1 method)

In [2]: RZ(2π)
Out[2]: 2×2 Matrix{ComplexF64}:
 -1.0-1.22465e-16im   0.0+0.0im
  0.0+0.0im          -1.0+1.22465e-16im

def_ry.append(inst, [0], [])
_eagle_sel.add_equivalence(RYGate(theta), def_ry)

# PhaseGate
q = QuantumRegister(1, "q")
def_p = QuantumCircuit(q)
theta = Parameter("theta")
def_p.append(RZGate(theta), [0], [])
_eagle_sel.add_equivalence(PhaseGate(theta), def_p)
Loading
Loading