Skip to content

Commit

Permalink
Fix global phase handling in circuit.repeat (#5588) (#5592)
Browse files Browse the repository at this point in the history
* global phase should not be propagated in repeat

* add reno

(cherry picked from commit 7e40ed1)

Co-authored-by: Julien Gacon <gaconju@gmail.com>
  • Loading branch information
mergify[bot] and Cryoris authored Jan 7, 2021
1 parent 2951828 commit ae90e27
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
3 changes: 1 addition & 2 deletions qiskit/circuit/quantumcircuit.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,7 @@ def repeat(self, reps):
QuantumCircuit: A circuit containing ``reps`` repetitions of this circuit.
"""
repeated_circ = QuantumCircuit(*self.qregs, *self.cregs,
name=self.name + '**{}'.format(reps),
global_phase=reps * self.global_phase)
name=self.name + '**{}'.format(reps))

# benefit of appending instructions: decomposing shows the subparts, i.e. the power
# is actually `reps` times this circuit, and it is currently much faster than `compose`.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
fixes:
- |
Fix the global phase of the output of ``circuit.repeat``. If a circuit with global
phase is appended to another circuit, the global phase is currently not propagated.
The simulators rely on this, since the phase otherwise gets applied multiple times.
This sets the global phase of ``circuit.repeat`` to 0 instead of multiplying the existing
phase times the number of repetitions.
10 changes: 10 additions & 0 deletions test/python/circuit/test_circuit_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
"""Test Qiskit's QuantumCircuit class."""

from ddt import ddt, data
import numpy as np
from qiskit import BasicAer
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import execute
from qiskit.circuit import Gate, Instruction, Parameter
from qiskit.circuit.exceptions import CircuitError
from qiskit.test import QiskitTestCase
from qiskit.circuit.library.standard_gates import SGate
from qiskit.quantum_info import Operator


@ddt
Expand Down Expand Up @@ -403,6 +405,14 @@ def test_repeat(self):
rep = qc.repeat(3)
self.assertEqual(rep, ref)

@data(0, 1, 4)
def test_repeat_global_phase(self, num):
"""Test the global phase is properly handled upon repeat."""
phase = 0.123
qc = QuantumCircuit(1, global_phase=phase)
expected = np.exp(1j * phase * num) * np.identity(2)
np.testing.assert_array_almost_equal(Operator(qc.repeat(num)).data, expected)

def test_power(self):
"""Test taking the circuit to a power works."""
qc = QuantumCircuit(2)
Expand Down

0 comments on commit ae90e27

Please sign in to comment.