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

Support QuantumCircuit.initialize(int, qubits[]) #1841

Merged
merged 3 commits into from
Jun 12, 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
8 changes: 6 additions & 2 deletions qiskit_aer/backends/aer_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,19 @@ def _inline_initialize(self, circ, optype):
return circ

for inst, _, _ in circ.data:
if isinstance(inst, Initialize) and not isinstance(inst.params[0], complex):
if isinstance(inst, Initialize) and (
(not isinstance(inst.params[0], complex)) or (len(inst.params) == 1)
):
break
else:
return circ

new_circ = circ.copy()
new_circ.data = []
for inst, qargs, cargs in circ.data:
if isinstance(inst, Initialize) and not isinstance(inst.params[0], complex):
if isinstance(inst, Initialize) and (
(not isinstance(inst.params[0], complex)) or (len(inst.params) == 1)
):
# Assume that the decomposed circuit of inst.definition consists of basis gates
new_circ.compose(inst.definition.decompose(), qargs, cargs, inplace=True)
else:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
fixes:
- |
:meth:``QuantumCircuit.initialize()`` with `int` value was not processed
correctly as reported in `#1821 <https://github.com/Qiskit/qiskit-aer/issues/1821>`.
This commit enables such initialization by decomposing initialize instructions.
31 changes: 31 additions & 0 deletions test/terra/backends/aer_simulator/test_initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,34 @@ def test_initialize_with_labels(self, method, device):
for q1, p1 in enumerate([1, -1j]):
index = int("{}{}{}{}".format(q4, q3, q2, q1), 2)
self.assertAlmostEqual(actual[index], 0.25 * p1 * p2 * p3 * p4)

@supported_methods(SUPPORTED_METHODS)
def test_initialize_with_int(self, method, device):
"""Test sampling with int"""
backend = self.backend(method=method, device=device)

circ = QuantumCircuit(4)
circ.initialize(5, [0, 1, 2])
circ.save_statevector()
if "tensor_network" in method:
actual = backend.run(circ, shots=50).result().get_statevector(circ)
else:
actual = backend.run(circ).result().get_statevector(circ)

self.assertAlmostEqual(actual[5], 1)

@supported_methods(SUPPORTED_METHODS)
def test_initialize_with_int_twice(self, method, device):
"""Test sampling with int twice"""
backend = self.backend(method=method, device=device)

circ = QuantumCircuit(4)
circ.initialize(1, [0])
circ.initialize(1, [2])
circ.save_statevector()
if "tensor_network" in method:
actual = backend.run(circ, shots=50).result().get_statevector(circ)
else:
actual = backend.run(circ).result().get_statevector(circ)

self.assertAlmostEqual(actual[5], 1)