Skip to content

Commit 4420e28

Browse files
madcpfrht
authored andcommitted
Add qubits as a param of PauliSum.matrix() (quantumlib#4203)
1 parent 599520e commit 4420e28

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

cirq-core/cirq/ops/linear_combinations.py

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from collections import defaultdict
1515
from typing import (
1616
AbstractSet,
17+
Iterable,
1718
Mapping,
1819
Optional,
1920
Tuple,
@@ -416,18 +417,21 @@ def copy(self) -> 'PauliSum':
416417
factory = type(self)
417418
return factory(self._linear_dict.copy())
418419

419-
def matrix(self) -> np.ndarray:
420-
"""Reconstructs matrix of self from underlying Pauli operations.
420+
def matrix(self, qubits: Optional[Iterable[raw_types.Qid]] = None) -> np.ndarray:
421+
"""Reconstructs matrix of self from underlying Pauli operations in
422+
computational basis of qubits.
421423
422424
Raises:
423425
TypeError: if any of the gates in self does not provide a unitary.
424426
"""
425-
num_qubits = len(self.qubits)
427+
428+
qubits = self.qubits if qubits is None else tuple(qubits)
429+
num_qubits = len(qubits)
426430
num_dim = 2 ** num_qubits
427431
result = np.zeros((num_dim, num_dim), dtype=np.complex128)
428432
for vec, coeff in self._linear_dict.items():
429433
op = _pauli_string_from_unit(vec)
430-
result += coeff * op.matrix(self.qubits)
434+
result += coeff * op.matrix(qubits)
431435
return result
432436

433437
def _has_unitary_(self) -> bool:

cirq-core/cirq/ops/linear_combinations_test.py

+30
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,7 @@ def test_non_pauli_sum_has_no_unitary(psum):
10351035
(cirq.Z(q1), (q1,)),
10361036
(cirq.X(q0) + cirq.Y(q0), (q0,)),
10371037
(cirq.X(q0) + cirq.Y(q2), (q0, q2)),
1038+
(cirq.X(q2) + cirq.Y(q0), (q0, q2)),
10381039
(cirq.X(q0) * cirq.Y(q1) + cirq.Y(q1) * cirq.Z(q3), (q0, q1, q3)),
10391040
),
10401041
)
@@ -1189,6 +1190,35 @@ def test_pauli_sum_formatting():
11891190
assert str(empty) == "0.000"
11901191

11911192

1193+
def test_pauli_sum_matrix():
1194+
q = cirq.LineQubit.range(3)
1195+
paulisum = cirq.X(q[0]) * cirq.X(q[1]) + cirq.Z(q[0])
1196+
H1 = np.array(
1197+
[[1.0, 0.0, 0.0, 1.0], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, -1.0, 0.0], [1.0, 0.0, 0.0, -1.0]]
1198+
)
1199+
assert np.allclose(H1, paulisum.matrix())
1200+
assert np.allclose(H1, paulisum.matrix([q[0], q[1]]))
1201+
# Expects a different matrix when change qubits order.
1202+
H2 = np.array(
1203+
[[1.0, 0.0, 0.0, 1.0], [0.0, -1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0], [1.0, 0.0, 0.0, -1.0]]
1204+
)
1205+
assert np.allclose(H2, paulisum.matrix([q[1], q[0]]))
1206+
# Expects matrix with a different size when add a new qubit.
1207+
H3 = np.array(
1208+
[
1209+
[1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0],
1210+
[0.0, -1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
1211+
[0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0],
1212+
[0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0],
1213+
[0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0],
1214+
[1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0],
1215+
[0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0],
1216+
[0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0],
1217+
]
1218+
)
1219+
assert np.allclose(H3, paulisum.matrix([q[1], q[2], q[0]]))
1220+
1221+
11921222
def test_pauli_sum_repr():
11931223
q = cirq.LineQubit.range(2)
11941224
pstr1 = cirq.X(q[0]) * cirq.X(q[1])

0 commit comments

Comments
 (0)