-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #272 from QunaSys/check_operator_estimatable
Check operator estimatable
- Loading branch information
Showing
4 changed files
with
177 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# 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 | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from quri_parts.core.estimator import Estimatable | ||
from quri_parts.core.operator import PAULI_IDENTITY, Operator, PauliLabel, zero | ||
from quri_parts.core.state import QuantumState | ||
|
||
|
||
def is_estimatable(observable: Estimatable, state: QuantumState) -> bool: | ||
"""Check if the qubit count of the observable is larger than that of the | ||
state.""" | ||
if observable == PAULI_IDENTITY or observable == zero(): | ||
return True | ||
|
||
elif isinstance(observable, PauliLabel): | ||
min_state_qubit_required = max(observable.qubit_indices()) + 1 | ||
return min_state_qubit_required <= state.qubit_count | ||
|
||
elif isinstance(observable, Operator): | ||
for op in observable: | ||
if not is_estimatable(op, state): | ||
return False | ||
return True | ||
|
||
else: | ||
assert False, "Observable should be either a PauliLabel or an Operator." |
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,78 @@ | ||
# 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 | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from quri_parts.circuit import UnboundParametricQuantumCircuit | ||
from quri_parts.core.estimator import Estimatable | ||
from quri_parts.core.estimator.utils import is_estimatable | ||
from quri_parts.core.operator import PAULI_IDENTITY, Operator, pauli_label, zero | ||
from quri_parts.core.state import ( | ||
ComputationalBasisState, | ||
GeneralCircuitQuantumState, | ||
ParametricCircuitQuantumState, | ||
ParametricQuantumStateVector, | ||
QuantumStateVector, | ||
) | ||
|
||
|
||
def test_is_estimatable() -> None: | ||
states = [ | ||
GeneralCircuitQuantumState(3), | ||
ComputationalBasisState(3), | ||
QuantumStateVector(3), | ||
ParametricCircuitQuantumState(3, circuit=UnboundParametricQuantumCircuit(3)), | ||
ParametricQuantumStateVector(3, circuit=UnboundParametricQuantumCircuit(3)), | ||
] | ||
|
||
valid_pauli_labels: list[Estimatable] = [ | ||
zero(), | ||
PAULI_IDENTITY, | ||
pauli_label("X0"), | ||
pauli_label("Y1"), | ||
pauli_label("Z2"), | ||
pauli_label("X0 Y2"), | ||
pauli_label("Y1 Z2"), | ||
pauli_label("Z2"), | ||
] | ||
|
||
for op in valid_pauli_labels: | ||
for state in states: | ||
assert is_estimatable(op, state) | ||
|
||
invalid_pauli_labels = [pauli_label("X3"), pauli_label("Z50000")] | ||
for op in invalid_pauli_labels: | ||
for state in states: | ||
assert not is_estimatable(op, state) | ||
|
||
valid_operator = Operator( | ||
{ | ||
pauli_label("Z0 Z1 Z2"): 1, | ||
pauli_label("X0 Z1 Y2"): 2, | ||
pauli_label("X0 Z1 X2"): 2, | ||
pauli_label("Z1"): 2, | ||
PAULI_IDENTITY: 3, | ||
} | ||
) | ||
|
||
for state in states: | ||
assert is_estimatable(valid_operator, state) | ||
|
||
invalid_operator = Operator( | ||
{ | ||
pauli_label("Z50000"): 1, | ||
pauli_label("Z3"): 1, | ||
pauli_label("Z0 Z1 Z2"): 1, | ||
pauli_label("X0 Z1 Y2"): 2, | ||
pauli_label("X0 Z1 X2"): 2, | ||
pauli_label("Z1"): 2, | ||
PAULI_IDENTITY: 3, | ||
} | ||
) | ||
for state in states: | ||
assert not is_estimatable(invalid_operator, state) |
5e97c2e
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.
🎉 Published on https://quri-parts.qunasys.com as production
🚀 Deployed on https://65681d2c1eaeb1656ee1b89e--quri-parts.netlify.app