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

Remove the constraint that stim.Tableau.from_state_vector inputs must be normalized #840

Merged
merged 4 commits into from
Oct 11, 2024
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
10 changes: 9 additions & 1 deletion src/stim/stabilizers/tableau.pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2130,8 +2130,16 @@ void stim_pybind::pybind_tableau_methods(pybind11::module &m, pybind11::class_<T
}

std::vector<std::complex<float>> v;
double weight = 0;
drqec marked this conversation as resolved.
Show resolved Hide resolved
for (const auto &obj : state_vector) {
v.push_back(pybind11::cast<std::complex<float>>(obj));
weight += std::norm(v.back());
}
if (weight != 1.0 && weight > 0.0) {
std::complex<float> scale = (float)sqrt(1.0 / weight);
for (auto &amplitude : v) {
amplitude *= scale;
}
}

return circuit_to_tableau<MAX_BITWORD_WIDTH>(
Expand All @@ -2147,7 +2155,7 @@ void stim_pybind::pybind_tableau_methods(pybind11::module &m, pybind11::class_<T
Args:
state_vector: A list of complex amplitudes specifying a superposition. The
vector must correspond to a state that is reachable using Clifford
operations, and must be normalized (i.e. it must be a unit vector).
operations.
drqec marked this conversation as resolved.
Show resolved Hide resolved
endian:
"little": state vector is in little endian order, where higher index
qubits correspond to larger changes in the state index.
Expand Down
23 changes: 21 additions & 2 deletions src/stim/stabilizers/tableau_pybind_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand Down Expand Up @@ -56,6 +54,27 @@ def test_from_named_gate():
with pytest.raises(IndexError, match="not unitary"):
stim.Tableau.from_named_gate("X_ERROR")

def test_from_state_vector():
drqec marked this conversation as resolved.
Show resolved Hide resolved
t = stim.Tableau.from_state_vector([
0.5**0.5,
0,
0,
0.5**0.5,
], endian='little')
assert len(t) == 2
assert t.x_output(0) == stim.PauliString("Z_")
assert t.x_output(1) == stim.PauliString("_X")
assert t.z_output(0) == stim.PauliString("XX")
assert t.z_output(1) == stim.PauliString("ZZ")

unnormalized = stim.Tableau.from_state_vector([
drqec marked this conversation as resolved.
Show resolved Hide resolved
1,
0,
0,
1
], endian='little')
assert unnormalized == t


def test_identity():
t = stim.Tableau(3)
Expand Down