Skip to content

Commit

Permalink
Alias BaseOperator.__mul__ to BaseOperator._multiply in quantum_info (#…
Browse files Browse the repository at this point in the history
…8007)

* add left multiplication

* add tests

* add releasenote

* fix incorrectly mixed module

* change method of __mul__

* fix lint

* Reword release note

Co-authored-by: Jake Lishman <[email protected]>
Co-authored-by: Ikko Hamamura <[email protected]>
  • Loading branch information
3 people authored May 9, 2022
1 parent cffbb84 commit ec9d693
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 2 deletions.
3 changes: 3 additions & 0 deletions qiskit/quantum_info/operators/mixins/multiply.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class MultiplyMixin(ABC):
def __rmul__(self, other):
return self._multiply(other)

def __mul__(self, other):
return self._multiply(other)

def __truediv__(self, other):
return self._multiply(1 / other)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
features:
- |
Classes in the :mod:`.quantum_info` module that support scalar multiplication
can now be multiplied by a scalar from either the left or the right.
Previously, they would only accept scalar multipliers from the left.
2 changes: 2 additions & 0 deletions test/python/quantum_info/operators/channel/test_chi.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ def test_multiply(self):
targ = Chi(val * self.chiI)
self.assertEqual(chan._multiply(val), targ)
self.assertEqual(val * chan, targ)
targ = Chi(self.chiI * val)
self.assertEqual(chan * val, targ)

def test_multiply_except(self):
"""Test multiply method raises exceptions."""
Expand Down
2 changes: 2 additions & 0 deletions test/python/quantum_info/operators/channel/test_choi.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ def test_multiply(self):
targ = Choi(val * self.choiI)
self.assertEqual(chan._multiply(val), targ)
self.assertEqual(val * chan, targ)
targ = Choi(self.choiI * val)
self.assertEqual(chan * val, targ)

def test_multiply_except(self):
"""Test multiply method raises exceptions."""
Expand Down
3 changes: 3 additions & 0 deletions test/python/quantum_info/operators/channel/test_kraus.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,9 @@ def test_multiply(self):
self.assertEqual(rho & chan, targ)
chan = val * chan1
self.assertEqual(rho & chan, targ)
targ = (rho & chan1) * val
chan = chan1 * val
self.assertEqual(rho & chan, targ)

# Double Kraus set
chan2 = Kraus((kraus1, kraus2))
Expand Down
2 changes: 2 additions & 0 deletions test/python/quantum_info/operators/channel/test_ptm.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,8 @@ def test_multiply(self):
targ = PTM(val * self.ptmI)
self.assertEqual(chan._multiply(val), targ)
self.assertEqual(val * chan, targ)
targ = PTM(self.ptmI * val)
self.assertEqual(chan * val, targ)

def test_multiply_except(self):
"""Test multiply method raises exceptions."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,9 @@ def test_multiply(self):
self.assertEqual(rho_init.evolve(chan), rho_targ)
chan = val * chan1
self.assertEqual(rho_init.evolve(chan), rho_targ)
rho_targ = (rho_init & chan1) * val
chan = chan1 * val
self.assertEqual(rho_init.evolve(chan), rho_targ)

# Double Stinespring set
chan2 = Stinespring((stine1, stine2), input_dims=2, output_dims=4)
Expand Down
2 changes: 2 additions & 0 deletions test/python/quantum_info/operators/channel/test_superop.py
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,8 @@ def test_multiply(self):
targ = SuperOp(val * self.sopI)
self.assertEqual(chan._multiply(val), targ)
self.assertEqual(val * chan, targ)
targ = SuperOp(self.sopI * val)
self.assertEqual(chan * val, targ)

def test_multiply_except(self):
"""Test multiply method raises exceptions."""
Expand Down
3 changes: 3 additions & 0 deletions test/python/quantum_info/operators/symplectic/test_pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ def test_multiply(self, val):
op = val * Pauli(([True, True], [False, False], 0))
phase = (-1j) ** op.phase
self.assertEqual(phase, val)
op = Pauli(([True, True], [False, False], 0)) * val
phase = (-1j) ** op.phase
self.assertEqual(phase, val)

def test_multiply_except(self):
"""Test multiply method raises exceptions."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,8 +450,13 @@ def test_mul(self, num_qubits, value):
spp_op = self.random_spp_op(num_qubits, 2**num_qubits)
target = value * Operator(spp_op)
op = value * spp_op
value = op.to_operator()
self.assertEqual(value, target)
value_mat = op.to_operator()
self.assertEqual(value_mat, target)
np.testing.assert_array_equal(op.paulis.phase, np.zeros(op.size))
target = Operator(spp_op) * value
op = spp_op * value
value_mat = op.to_operator()
self.assertEqual(value_mat, target)
np.testing.assert_array_equal(op.paulis.phase, np.zeros(op.size))

@combine(num_qubits=[1, 2, 3], value=[1, 1j, -3 + 4.4j])
Expand Down
1 change: 1 addition & 0 deletions test/python/quantum_info/operators/test_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,7 @@ def test_multiply(self):
op = Operator(mat)
self.assertEqual(op._multiply(val), Operator(val * mat))
self.assertEqual(val * op, Operator(val * mat))
self.assertEqual(op * val, Operator(mat * val))

def test_multiply_except(self):
"""Test multiply method raises exceptions."""
Expand Down
3 changes: 3 additions & 0 deletions test/python/quantum_info/operators/test_scalar_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ def test_multiply(self, coeff1, coeff2):
val = coeff2 * ScalarOp(dims, coeff=coeff1)
target = coeff1 * coeff2
self.assertScalarOp(val, dims, target)
val = ScalarOp(dims, coeff=coeff1) * coeff2
target = coeff2 * coeff1
self.assertScalarOp(val, dims, target)

@combine(coeff1=[0, 1, -3.1, 1 + 3j], coeff2=[-1, -5.1 - 2j])
def test_add(self, coeff1, coeff2):
Expand Down

0 comments on commit ec9d693

Please sign in to comment.