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

Improve the performance of the ProductFormula synthesizers #12724

Merged
merged 22 commits into from
Jul 8, 2024

Conversation

mrossinek
Copy link
Member

@mrossinek mrossinek commented Jul 4, 2024

Summary

This PR implements the suggestion made in #12021 (comment).

Details and comments

I ran a very simple benchmark to showcase the performance improvements of this PR. Below you can see that we get a ~30x performance improvement (note the logarithmic y axis).

benchmark

Here is the code which builds a heavy-hex graph of a given size, constructs an XYZ model Hamiltonian on its connectivity, and builds a LieTrotter and SuzukiTrotter circuit.
import sys
import time

import numpy as np
import rustworkx as rx
from qiskit import QuantumCircuit, transpile
from qiskit.circuit.library import PauliEvolutionGate
from qiskit.quantum_info import SparsePauliOp
from qiskit.synthesis import LieTrotter, SuzukiTrotter

for size in range(3, 10, 2):
    graph = rx.generators.heavy_hex_graph(size, multigraph=False)

    num_qubits = graph.num_nodes()

    sparse_pauli_list = []
    for edge in graph.edge_list():
        for p in ("XX", "YY", "ZZ"):
            sparse_pauli_list.append((p, [edge[0], edge[1]], 1.0))
    for qubit in range(num_qubits):
        for p in "XYZ":
            sparse_pauli_list.append((p, [qubit], 1.0))

    hamil = SparsePauliOp.from_sparse_list(sparse_pauli_list, num_qubits=num_qubits)

    print("starting LieTrotter with 10 reps", file=sys.stderr)
    t1 = time.time()
    circuit = QuantumCircuit(num_qubits)
    circuit.append(
        PauliEvolutionGate(hamil, time=0.5, synthesis=LieTrotter(reps=10)),
        qargs=circuit.qubits,
    )
    transpiled = transpile(circuit, basis_gates=["rz", "x", "sx", "cx"])
    t2 = time.time()
    lie = t2 - t1
    print(f"took {lie}s", file=sys.stderr)

    print("starting SuzukiTrotter with 10 reps", file=sys.stderr)
    t1 = time.time()
    circuit = QuantumCircuit(num_qubits)
    circuit.append(
        PauliEvolutionGate(hamil, time=0.5, synthesis=SuzukiTrotter(reps=10)),
        qargs=circuit.qubits,
    )
    transpiled = transpile(circuit, basis_gates=["rz", "x", "sx", "cx"])
    t2 = time.time()
    suzuki = t2 - t1
    print(f"took {suzuki}s", file=sys.stderr)

As part of this improvement, I also aligned the implementations of LieTrotter and SuzukiTrotter which partially diverged in #12021. I also handle the wrapping of the individually time-evolved Pauli terms in a new way, which results in a different (but imo improved) visualization of a circuit. Below you can see the same circuit drawn with Qiskit 1.1.1 and this PR, side by side:

Qiskit 1.1.1 This PR
circuit_qiskit_1 1 1 circuit_branch
Here is the code to plot the circuit above
import numpy as np
from qiskit import QuantumCircuit
from qiskit.circuit.library import PauliEvolutionGate
from qiskit.quantum_info import SparsePauliOp
from qiskit.synthesis import LieTrotter, SuzukiTrotter

num_qubits = 10
edges = [(i, i + 1) for i in range(0, num_qubits - 1, 2)]
edges += [(i, i + 1) for i in range(1, num_qubits - 1, 2)]

sparse_pauli_list = []
for edge in edges:
    for p in ("XX", "YY", "ZZ"):
        sparse_pauli_list.append((p, [edge[0], edge[1]], 1.0))
for qubit in range(num_qubits):
    for p in "XYZ":
        sparse_pauli_list.append((p, [qubit], 1.0))

hamil = SparsePauliOp.from_sparse_list(sparse_pauli_list, num_qubits=num_qubits)

circuit = QuantumCircuit(num_qubits)
circuit.append(
    PauliEvolutionGate(hamil, time=0.5, synthesis=LieTrotter(wrap=True)),
    qargs=circuit.qubits,
)
circuit = circuit.decompose()
circuit.draw("mpl", filename="circuit_branch.png")

This changes the structure of the `atomic_evolution` callable in the
`ProductFormula` synthesis class. This is motivated by the significant
performance improvements that can be obtained by appending to the
existing circuit directly rather than building out individual evolution
circuits and iteratively composing them.
This can be used to recover the previous behavior in which the single
individually evolved Pauli terms get wrapped into gate objects.
@qiskit-bot
Copy link
Collaborator

One or more of the following people are relevant to this code:

  • @Cryoris
  • @Qiskit/terra-core
  • @ajavadia

@mrossinek mrossinek changed the title Improves the performance of the ProductFormula synthesizers Improve the performance of the ProductFormula synthesizers Jul 4, 2024
@coveralls
Copy link

coveralls commented Jul 4, 2024

Pull Request Test Coverage Report for Build 9796156398

Details

  • 94 of 99 (94.95%) changed or added relevant lines in 6 files are covered.
  • 24 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.008%) to 89.824%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 3 4 75.0%
qiskit/synthesis/evolution/lie_trotter.py 11 12 91.67%
qiskit/synthesis/evolution/suzuki_trotter.py 9 10 90.0%
qiskit/synthesis/evolution/product_formula.py 62 64 96.88%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 6 91.35%
crates/qasm2/src/parse.rs 18 96.69%
Totals Coverage Status
Change from base Build 9794304676: -0.008%
Covered Lines: 65206
Relevant Lines: 72593

💛 - Coveralls

@Cryoris Cryoris self-assigned this Jul 5, 2024
Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

This looks great, both performance and visualization wise, thanks for the effort! 🙂 I just have a few minor comments below.

@Cryoris Cryoris added performance Changelog: Deprecation Include in "Deprecated" section of changelog Changelog: New Feature Include in the "Added" section of the changelog labels Jul 5, 2024
@mrossinek mrossinek requested a review from Cryoris July 5, 2024 10:30
@coveralls
Copy link

coveralls commented Jul 5, 2024

Pull Request Test Coverage Report for Build 9806942643

Details

  • 95 of 100 (95.0%) changed or added relevant lines in 6 files are covered.
  • 22 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.007%) to 89.835%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 3 4 75.0%
qiskit/synthesis/evolution/lie_trotter.py 11 12 91.67%
qiskit/synthesis/evolution/suzuki_trotter.py 10 11 90.91%
qiskit/synthesis/evolution/product_formula.py 62 64 96.88%
Files with Coverage Reduction New Missed Lines %
qiskit/transpiler/passes/synthesis/unitary_synthesis.py 2 88.35%
crates/qasm2/src/lex.rs 8 91.6%
crates/qasm2/src/parse.rs 12 97.15%
Totals Coverage Status
Change from base Build 9806112690: -0.007%
Covered Lines: 65214
Relevant Lines: 72593

💛 - Coveralls

This is slightly faster than the `.compose`-based operation done
previously as it performs fewer checks. Thanks to @jakelishman for the
suggestion offline.
@coveralls
Copy link

coveralls commented Jul 8, 2024

Pull Request Test Coverage Report for Build 9842266493

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 95 of 100 (95.0%) changed or added relevant lines in 6 files are covered.
  • 34 unchanged lines in 3 files lost coverage.
  • Overall coverage decreased (-0.07%) to 89.765%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 3 4 75.0%
qiskit/synthesis/evolution/lie_trotter.py 11 12 91.67%
qiskit/synthesis/evolution/suzuki_trotter.py 10 11 90.91%
qiskit/synthesis/evolution/product_formula.py 62 64 96.88%
Files with Coverage Reduction New Missed Lines %
crates/qasm2/src/lex.rs 5 92.11%
crates/qasm2/src/parse.rs 12 96.69%
crates/circuit/src/slice.rs 17 83.85%
Totals Coverage Status
Change from base Build 9825985118: -0.07%
Covered Lines: 59209
Relevant Lines: 65960

💛 - Coveralls

Co-authored-by: Julien Gacon <gaconju@gmail.com>
@mrossinek mrossinek requested a review from Cryoris July 8, 2024 15:02
Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

Great improvement, thanks a lot for the work! 🚤 💨

@Cryoris Cryoris enabled auto-merge July 8, 2024 15:17
@Cryoris Cryoris added this pull request to the merge queue Jul 8, 2024
Merged via the queue into Qiskit:main with commit 008dde3 Jul 8, 2024
15 checks passed
@mrossinek mrossinek deleted the local_atomic_evolution branch July 9, 2024 04:43
Procatv pushed a commit to Procatv/qiskit-terra-catherines that referenced this pull request Aug 1, 2024
…12724)

* [WIP] adds the output argument to the internal atomic evolution

* meta: modernize type hints

* refactor: change callable structure of atomic evolution

This changes the structure of the `atomic_evolution` callable in the
`ProductFormula` synthesis class. This is motivated by the significant
performance improvements that can be obtained by appending to the
existing circuit directly rather than building out individual evolution
circuits and iteratively composing them.

* refactor: deprecate the legacy atomic_evolution signature

* refactor: add the wrap argument to ProductFormula

This can be used to recover the previous behavior in which the single
individually evolved Pauli terms get wrapped into gate objects.

* fix: insert the missing barriers between LieTrotter repetitions

* refactor: align SuzukiTrotter and LieTrotter

* Propoagate deprecation notice

* fix: the labels of wrapped Pauli evolutions

* fix: respect the insert_barriers setting

* docs: add a release note

* Apply suggestions from code review

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* fix: missing `wrap` forward in SuzukiTrotter

* docs: improve documentation of the `atomic_evolution` argument

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* docs: also document the deprecated form of `atomic_evolution`

* docs: call out ProductFormula docs in release note

* refactor: change to PendingDeprecationWarning

* refactor: explicitly convert to Gate when wrapping

This is slightly faster than the `.compose`-based operation done
previously as it performs fewer checks. Thanks to @jakelishman for the
suggestion offline.

* Update qiskit/synthesis/evolution/lie_trotter.py

Co-authored-by: Julien Gacon <gaconju@gmail.com>

* docs: update after pending deprecation

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: Deprecation Include in "Deprecated" section of changelog Changelog: New Feature Include in the "Added" section of the changelog performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants