Skip to content

Commit 6cd3910

Browse files
committed
address code review comments
1 parent 1460b9c commit 6cd3910

File tree

3 files changed

+92
-7
lines changed

3 files changed

+92
-7
lines changed

pennylane/estimator/compact_hamiltonian.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
Contains classes used to compactly store the metadata of various Hamiltonians which are relevant for resource estimation.
1616
"""
1717
import copy
18-
from collections import defaultdict
18+
from collections import Counter
1919
from dataclasses import dataclass
2020

2121

@@ -321,7 +321,7 @@ def __init__( # pylint: disable=too-many-arguments
321321
self._num_qubits = num_qubits
322322
self._one_norm = one_norm
323323

324-
(max_weight, num_pauli_words, pauli_dist, commuting_groups) = _validate_inputs(
324+
(max_weight, num_pauli_words, pauli_dist, commuting_groups) = _preprocess_inputs(
325325
num_qubits, num_pauli_words, max_weight, pauli_dist, commuting_groups
326326
)
327327

@@ -428,7 +428,7 @@ def _pauli_dist_from_commuting_groups(commuting_groups: tuple[dict]):
428428
return total_pauli_dist
429429

430430

431-
def _validate_inputs(
431+
def _preprocess_inputs(
432432
num_qubits: int,
433433
num_pauli_words: int | None,
434434
max_weight: int | None,

pennylane/estimator/templates/trotter.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1930,6 +1930,16 @@ def __init__(
19301930
"Unsupported Hamiltonian representation for TrotterPauli."
19311931
f"This method works with PauliHamiltonian, {type(pauli_ham)} provided"
19321932
)
1933+
if (not isinstance(num_steps, int)) or num_steps < 1:
1934+
raise ValueError(
1935+
f"`num_steps` is expected to be a positive integer greater than one, got {num_steps}"
1936+
)
1937+
1938+
if not (isinstance(order, int) and order > 0 and (order == 1 or order % 2 == 0)):
1939+
raise ValueError(
1940+
f"`order` is expected to be a positive integer and either one or a multiple of two; got {order}"
1941+
)
1942+
19331943
self.num_steps = num_steps
19341944
self.order = order
19351945
self.pauli_ham = pauli_ham
@@ -2097,7 +2107,7 @@ def cost_pauli_group(pauli_dist: dict):
20972107
a commuting group of Pauli words.
20982108
20992109
"""
2100-
return [
2101-
GateCount(PauliRot.resource_rep(pauli_word), count)
2102-
for pauli_word, count in pauli_dist.items()
2103-
]
2110+
return [
2111+
GateCount(PauliRot.resource_rep(pauli_word), count)
2112+
for pauli_word, count in pauli_dist.items()
2113+
]

tests/estimator/templates/test_estimator_trotter.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,81 @@ def test_initialization(self, pauli_ham, num_steps, order, wires, expected_num_w
808808
ValueError,
809809
"Expected 5 wires, got 4",
810810
),
811+
(
812+
qre.PauliHamiltonian(
813+
num_qubits=5,
814+
commuting_groups=(
815+
{"XX": 15, "X": 5},
816+
{"ZZ": 10},
817+
{"YY": 5, "X": 5},
818+
),
819+
),
820+
10,
821+
3,
822+
["w1", "w2", "w3", "w4", "w5"],
823+
ValueError,
824+
"`order` is expected to be a positive integer and either one or a multiple of two",
825+
),
826+
(
827+
qre.PauliHamiltonian(
828+
num_qubits=5,
829+
commuting_groups=(
830+
{"XX": 15, "X": 5},
831+
{"ZZ": 10},
832+
{"YY": 5, "X": 5},
833+
),
834+
),
835+
10,
836+
2.0,
837+
["w1", "w2", "w3", "w4", "w5"],
838+
ValueError,
839+
"`order` is expected to be a positive integer and either one or a multiple of two",
840+
),
841+
(
842+
qre.PauliHamiltonian(
843+
num_qubits=5,
844+
commuting_groups=(
845+
{"XX": 15, "X": 5},
846+
{"ZZ": 10},
847+
{"YY": 5, "X": 5},
848+
),
849+
),
850+
10,
851+
-2,
852+
["w1", "w2", "w3", "w4", "w5"],
853+
ValueError,
854+
"`order` is expected to be a positive integer and either one or a multiple of two",
855+
),
856+
(
857+
qre.PauliHamiltonian(
858+
num_qubits=5,
859+
commuting_groups=(
860+
{"XX": 15, "X": 5},
861+
{"ZZ": 10},
862+
{"YY": 5, "X": 5},
863+
),
864+
),
865+
10.5,
866+
4,
867+
["w1", "w2", "w3", "w4", "w5"],
868+
ValueError,
869+
"`num_steps` is expected to be a positive integer greater than one",
870+
),
871+
(
872+
qre.PauliHamiltonian(
873+
num_qubits=5,
874+
commuting_groups=(
875+
{"XX": 15, "X": 5},
876+
{"ZZ": 10},
877+
{"YY": 5, "X": 5},
878+
),
879+
),
880+
-5,
881+
4,
882+
["w1", "w2", "w3", "w4", "w5"],
883+
ValueError,
884+
"`num_steps` is expected to be a positive integer greater than one",
885+
),
811886
),
812887
)
813888
def test_init_raises_error_incompatible_inputs(

0 commit comments

Comments
 (0)