-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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 circuit generation from set of stabilizers #11483
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7fb778e
Add stabilizer_to_circuit function
Randl e0e1ffc
Fix verification test
Randl 0f7f185
Mention `stabilizer_to_circuit` in `StabilizerState` documentation
Randl dbf60d5
* Add `from_stabilizer_list` function to Stabilizer state
Randl 0a4ca53
doc fixes
Randl 69dddc2
Allow any Collection and not just list, fix naming, tests, and docs a…
Randl 80dfce6
typo
Randl 8ad1da2
missed rename
Randl 79de2a7
Documentation fixes
Randl 301c3a2
fix indent
Randl d177030
fix indent 2
Randl b037653
fix indent 3
Randl 7e4e12b
Rewrite using `Pauli` instead of string.
Randl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
# This code is part of Qiskit. | ||
# | ||
# (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. | ||
""" | ||
Stabilizer to circuit function | ||
""" | ||
from __future__ import annotations | ||
|
||
from collections.abc import Collection | ||
|
||
import numpy as np | ||
|
||
from qiskit.quantum_info import PauliList | ||
from qiskit.exceptions import QiskitError | ||
from qiskit.circuit import QuantumCircuit | ||
from qiskit.quantum_info.operators.symplectic.clifford import Clifford | ||
|
||
|
||
def synth_circuit_from_stabilizers( | ||
stabilizers: Collection[str], | ||
allow_redundant: bool = False, | ||
allow_underconstrained: bool = False, | ||
invert: bool = False, | ||
) -> QuantumCircuit: | ||
# pylint: disable=line-too-long | ||
"""Synthesis of a circuit that generates a state stabilized by the stabilziers | ||
using Gaussian elimination with Clifford gates. | ||
If the stabilizers are underconstrained, and `allow_underconstrained` is `True`, | ||
the circuit will output one of the states stabilized by the stabilizers. | ||
Based on stim implementation. | ||
|
||
Args: | ||
stabilizers (Collection[str]): list of stabilizer strings | ||
allow_redundant (bool): allow redundant stabilizers (i.e., some stabilizers | ||
can be products of the others) | ||
allow_underconstrained (bool): allow underconstrained set of stabilizers (i.e., | ||
the stabilizers do not specify a unique state) | ||
invert (bool): return inverse circuit | ||
Randl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Return: | ||
QuantumCircuit: a circuit that generates a state stabilized by `stabilizers`. | ||
|
||
Raises: | ||
QiskitError: if the stabilizers are invalid, do not commute, or contradict each other, | ||
if the list is underconstrained and `allow_underconstrained` is `False`, | ||
or if the list is redundant and `allow_redundant` is `False`. | ||
|
||
Reference: | ||
1. https://github.com/quantumlib/Stim/blob/c0dd0b1c8125b2096cd54b6f72884a459e47fe3e/src/stim/stabilizers/conversions.inl#L469 | ||
2. https://quantumcomputing.stackexchange.com/questions/12721/how-to-calculate-destabilizer-group-of-toric-and-other-codes | ||
|
||
""" | ||
stabilizer_list = PauliList(stabilizers) | ||
if np.any(stabilizer_list.phase % 2): | ||
raise QiskitError("Some stabilizers have an invalid phase") | ||
if len(stabilizer_list.commutes_with_all(stabilizer_list)) < len(stabilizer_list): | ||
raise QiskitError("Some stabilizers do not commute.") | ||
|
||
num_qubits = stabilizer_list.num_qubits | ||
circuit = QuantumCircuit(num_qubits) | ||
|
||
used = 0 | ||
for i in range(len(stabilizer_list)): | ||
curr_stab = stabilizer_list[i].evolve(Clifford(circuit), frame="s") | ||
|
||
# Find pivot. | ||
pivot = used | ||
while pivot < num_qubits: | ||
if curr_stab[pivot].x or curr_stab[pivot].z: | ||
break | ||
pivot += 1 | ||
|
||
if pivot == num_qubits: | ||
if curr_stab.x.any(): | ||
raise QiskitError( | ||
f"Stabilizer {i} ({stabilizer_list[i]}) anti-commutes with some of " | ||
"the previous stabilizers." | ||
) | ||
if curr_stab.phase == 2: | ||
raise QiskitError( | ||
f"Stabilizer {i} ({stabilizer_list[i]}) contradicts " | ||
"some of the previous stabilizers." | ||
) | ||
if curr_stab.z.any() and not allow_redundant: | ||
raise QiskitError( | ||
f"Stabilizer {i} ({stabilizer_list[i]}) is a product of the others " | ||
"and allow_redundant is False. Add allow_redundant=True " | ||
"to the function call if you want to allow redundant stabilizers." | ||
) | ||
continue | ||
|
||
# Change pivot basis to the Z axis. | ||
if curr_stab[pivot].x: | ||
if curr_stab[pivot].z: | ||
circuit.h(pivot) | ||
circuit.s(pivot) | ||
circuit.h(pivot) | ||
circuit.s(pivot) | ||
circuit.s(pivot) | ||
else: | ||
circuit.h(pivot) | ||
|
||
# Cancel other terms in Pauli string. | ||
for j in range(num_qubits): | ||
if j == pivot or not (curr_stab[j].x or curr_stab[j].z): | ||
continue | ||
p = curr_stab[j].x + curr_stab[j].z * 2 | ||
if p == 1: # X | ||
circuit.h(pivot) | ||
circuit.cx(pivot, j) | ||
circuit.h(pivot) | ||
elif p == 2: # Z | ||
circuit.cx(j, pivot) | ||
elif p == 3: # Y | ||
circuit.h(pivot) | ||
circuit.s(j) | ||
circuit.s(j) | ||
circuit.s(j) | ||
circuit.cx(pivot, j) | ||
circuit.h(pivot) | ||
circuit.s(j) | ||
|
||
# Move pivot to diagonal. | ||
if pivot != used: | ||
circuit.swap(pivot, used) | ||
|
||
# fix sign | ||
curr_stab = stabilizer_list[i].evolve(Clifford(circuit), frame="s") | ||
if curr_stab.phase == 2: | ||
circuit.x(used) | ||
used += 1 | ||
|
||
if used < num_qubits and not allow_underconstrained: | ||
raise QiskitError( | ||
"Stabilizers are underconstrained and allow_underconstrained is False." | ||
" Add allow_underconstrained=True to the function call " | ||
"if you want to allow underconstrained stabilizers." | ||
) | ||
if invert: | ||
return circuit | ||
return circuit.inverse() |
14 changes: 14 additions & 0 deletions
14
releasenotes/notes/add-synth-circuit-from-stabilizer-list-4cf9cfa01bbc7ddf.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
features: | ||
- | | ||
Add :func:`qiskit.synthesis.synth_circuit_from_stabilizers` function that, given stabilizers, | ||
returns a circuit that outputs the state stabilized by the stabilizers. | ||
|
||
- | | ||
Add :meth:`qiskit.quantum_info.StabilizerState.from_stabilizer_list` method | ||
that generates a stabilizer state from a list of stabilizers:: | ||
|
||
from qiskit.quantum_info import StabilizerState | ||
|
||
stabilizer_list = ["ZXX", "-XYX", "+ZYY"] | ||
stab = StabilizerState.from_stabilizer_list(stabilizer_list) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually use
Returns:
instead ofReturn:
(though I do seeReturn:
in multiple places in the code as well). Let me address this to a higher autority, @jakelishman.