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

Add apply_initialize to AerState #2196

Merged
merged 5 commits into from
Jul 18, 2024
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_aer/backends/aer_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ class AerSimulator(AerBackend):
"while_loop",
"break_loop",
"continue_loop",
"initialize",
"reset",
"switch_case",
"delay",
Expand Down Expand Up @@ -575,6 +576,7 @@ class AerSimulator(AerBackend):
"while_loop",
"break_loop",
"continue_loop",
"initialize",
"reset",
"switch_case",
"delay",
Expand Down Expand Up @@ -655,6 +657,7 @@ class AerSimulator(AerBackend):
"save_statevector_dict",
"set_statevector",
"set_density_matrix",
"initialize",
"reset",
"switch_case",
"delay",
Expand Down
3 changes: 0 additions & 3 deletions qiskit_aer/backends/backend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
"unitary",
"diagonal",
"multiplexer",
"initialize",
"pauli",
"mcx_gray",
"ecr",
Expand Down Expand Up @@ -197,7 +196,6 @@
"csx",
"cswap",
"diagonal",
"initialize",
"ecr",
"store",
]
Expand Down Expand Up @@ -414,7 +412,6 @@
"unitary",
"diagonal",
"multiplexer",
"initialize",
"pauli",
"mcx_gray",
"ecr",
Expand Down
9 changes: 7 additions & 2 deletions qiskit_aer/quantum_info/states/aer_densitymatrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ def _from_instruction(inst, init_data, configs):
aer_state.configure(config_key, config_value)

basis_gates = BASIS_GATES["density_matrix"]
custom_insts = ["reset", "kraus", "barrier"]

aer_state.allocate_qubits(inst.num_qubits)
num_qubits = inst.num_qubits
Expand All @@ -254,9 +255,13 @@ def _from_instruction(inst, init_data, configs):
aer_state.apply_global_phase(inst.global_phase)

if isinstance(inst, QuantumCircuit):
AerStatevector._aer_evolve_circuit(aer_state, inst, range(num_qubits), basis_gates)
AerStatevector._aer_evolve_circuit(
aer_state, inst, range(num_qubits), basis_gates, custom_insts
)
else:
AerStatevector._aer_evolve_instruction(aer_state, inst, range(num_qubits), basis_gates)
AerStatevector._aer_evolve_instruction(
aer_state, inst, range(num_qubits), basis_gates, custom_insts
)

return aer_state.move_to_ndarray(), aer_state

Expand Down
7 changes: 7 additions & 0 deletions qiskit_aer/quantum_info/states/aer_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,13 @@ def apply_measure(self, qubits):
# measure and update state
return self._native_state.apply_measure(qubits)

def apply_initialize(self, qubits, vec):
"""apply an initialize operation."""
self._assert_allocated_or_mapped()
self._assert_in_allocated_qubits(qubits)
# update state
return self._native_state.apply_initialize(qubits, vec)

def apply_reset(self, qubits):
"""apply a reset operation."""
self._assert_allocated_or_mapped()
Expand Down
37 changes: 26 additions & 11 deletions qiskit_aer/quantum_info/states/aer_statevector.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def _from_instruction(inst, init_data, configs):
aer_state.configure("method", method)

basis_gates = BASIS_GATES[method]
custom_insts = ["reset", "kraus", "barrier"]
if method == "statevector":
custom_insts.append("initialize")

aer_state.allocate_qubits(inst.num_qubits)
num_qubits = inst.num_qubits
Expand All @@ -188,14 +191,18 @@ def _from_instruction(inst, init_data, configs):
aer_state.apply_global_phase(inst.global_phase)

if isinstance(inst, QuantumCircuit):
AerStatevector._aer_evolve_circuit(aer_state, inst, range(num_qubits), basis_gates)
AerStatevector._aer_evolve_circuit(
aer_state, inst, range(num_qubits), basis_gates, custom_insts
)
else:
AerStatevector._aer_evolve_instruction(aer_state, inst, range(num_qubits), basis_gates)
AerStatevector._aer_evolve_instruction(
aer_state, inst, range(num_qubits), basis_gates, custom_insts
)

return aer_state.move_to_ndarray(), aer_state

@staticmethod
def _aer_evolve_circuit(aer_state, circuit, qubits, basis_gates=None):
def _aer_evolve_circuit(aer_state, circuit, qubits, basis_gates=None, custom_insts=None):
"""Apply circuit into aer_state"""
for instruction in circuit.data:
if instruction.clbits:
Expand All @@ -209,10 +216,11 @@ def _aer_evolve_circuit(aer_state, circuit, qubits, basis_gates=None):
inst,
[qubits[circuit.find_bit(qarg).index] for qarg in qargs],
basis_gates,
custom_insts,
)

@staticmethod
def _aer_evolve_instruction(aer_state, inst, qubits, basis_gates=None):
def _aer_evolve_instruction(aer_state, inst, qubits, basis_gates=None, custom_insts=None):
"""Apply instruction into aer_state"""

params = inst.params
Expand Down Expand Up @@ -260,20 +268,27 @@ def _aer_evolve_instruction(aer_state, inst, qubits, basis_gates=None):
pass
else:
applied = False
elif inst.name == "reset":
aer_state.apply_reset(qubits)
elif inst.name == "kraus":
aer_state.apply_kraus(qubits, inst.params)
elif inst.name == "barrier":
pass
elif custom_insts and inst.name in custom_insts:
if inst.name == "initialize":
aer_state.apply_initialize(qubits, inst.params)
elif inst.name == "reset":
aer_state.apply_reset(qubits)
elif inst.name == "kraus":
aer_state.apply_kraus(qubits, inst.params)
elif inst.name == "barrier":
pass
else:
applied = False
else:
applied = False

if not applied:
definition = inst.definition
if definition is inst or definition is None:
raise AerError("cannot decompose " + inst.name)
AerStatevector._aer_evolve_circuit(aer_state, definition, qubits, basis_gates)
AerStatevector._aer_evolve_circuit(
aer_state, definition, qubits, basis_gates, custom_insts
)

@classmethod
def from_label(cls, label):
Expand Down
Loading