Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIx 0q operation handling in Statevector (backport #10031) #10039

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions qiskit/quantum_info/operators/op_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ def num_qargs(self):
@property
def shape(self):
"""Return a tuple of the matrix shape"""
if self._num_qargs_l == self._num_qargs_r == 0:
# Scalar shape is op-like
return (1, 1)
if not self._num_qargs_r:
# Vector shape
return (self._dim_l,)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
Construction of a :class:`~.quantum_info.Statevector` from a :class:`.QuantumCircuit` containing
zero-qubit operations will no longer raise an error. These operations impart a global phase on
the resulting statevector.
9 changes: 8 additions & 1 deletion test/python/quantum_info/states/test_statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from qiskit import QiskitError
from qiskit import QuantumRegister, QuantumCircuit
from qiskit import transpile
from qiskit.circuit.library import HGate, QFT
from qiskit.circuit.library import HGate, QFT, GlobalPhaseGate
from qiskit.providers.basicaer import QasmSimulatorPy

from qiskit.quantum_info.random import random_unitary, random_statevector, random_pauli
Expand Down Expand Up @@ -186,6 +186,13 @@ def test_from_circuit(self):
psi = Statevector.from_instruction(circuit)
self.assertEqual(psi, target)

# Test 0q instruction
target = Statevector([1j, 0])
circuit = QuantumCircuit(1)
circuit.append(GlobalPhaseGate(np.pi / 2), [], [])
psi = Statevector.from_instruction(circuit)
self.assertEqual(psi, target)

def test_from_instruction(self):
"""Test initialization from an instruction."""
target = np.dot(HGate().to_matrix(), [1, 0])
Expand Down