Skip to content

Commit

Permalink
Fix converting VectorStateFn to a CircuitStateFn (#8405)
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryoris committed Jul 27, 2022
1 parent f8f638a commit 625ff98
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
5 changes: 2 additions & 3 deletions qiskit/opflow/state_fns/circuit_state_fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
from qiskit import BasicAer, ClassicalRegister, QuantumCircuit, transpile
from qiskit.circuit import Instruction, ParameterExpression
from qiskit.circuit.exceptions import CircuitError
from qiskit.circuit.library import IGate
from qiskit.extensions import Initialize
from qiskit.circuit.library import IGate, StatePreparation
from qiskit.opflow.exceptions import OpflowError
from qiskit.opflow.list_ops.composed_op import ComposedOp
from qiskit.opflow.list_ops.list_op import ListOp
Expand Down Expand Up @@ -123,7 +122,7 @@ def from_vector(statevector: np.ndarray) -> "CircuitStateFn":
"""
normalization_coeff = np.linalg.norm(statevector)
normalized_sv = statevector / normalization_coeff
return CircuitStateFn(Initialize(normalized_sv), coeff=normalization_coeff)
return CircuitStateFn(StatePreparation(normalized_sv), coeff=normalization_coeff)

def primitive_strings(self) -> Set[str]:
return {"QuantumCircuit"}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
fixes:
- |
Previously it was not possible to adjoint a :class:`.CircuitStateFn` that has been
constructed from a :class:`.VectorStateFn`. That's because the statevector has been
converted to a circuit with the :class:`~qiskit.extensions.Initialize` instruction, which
is not unitary. This problem is now fixed by instead using the :class:`.StatePreparation`
instruction, which can be used since the state is assumed to start out in the all 0 state.
For example we can now do::
from qiskit import QuantumCircuit
from qiskit.opflow import StateFn
left = StateFn([0, 1])
left_circuit = left.to_circuit_op().primitive
right_circuit = QuantumCircuit(1)
right_circuit.x(0)
overlap = left_circuit.inverse().compose(right_circuit) # this line raised an error before!
13 changes: 13 additions & 0 deletions test/python/opflow/test_state_op_meas_evals.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from qiskit.utils import QuantumInstance
from qiskit.opflow import StateFn, Zero, One, H, X, I, Z, Plus, Minus, CircuitSampler, ListOp
from qiskit.opflow.exceptions import OpflowError
from qiskit.quantum_info import Statevector


@ddt
Expand Down Expand Up @@ -217,6 +218,18 @@ def test_quantum_instance_with_backend_shots(self):
res = sampler.convert(~Plus @ Plus).eval()
self.assertAlmostEqual(res, 1 + 0j, places=2)

def test_adjoint_vector_to_circuit_fn(self):
"""Test it is possible to adjoint a VectorStateFn that was converted to a CircuitStateFn."""
left = StateFn([0, 1])
left_circuit = left.to_circuit_op().primitive

right_circuit = QuantumCircuit(1)
right_circuit.x(0)

circuit = left_circuit.inverse().compose(right_circuit)

self.assertTrue(Statevector(circuit).equiv([1, 0]))


if __name__ == "__main__":
unittest.main()

0 comments on commit 625ff98

Please sign in to comment.