Skip to content

Commit

Permalink
Add apply_initialize to AerState (#2196)
Browse files Browse the repository at this point in the history
* Add apply_initialize to AerState, move initialize from BASIS_GATES to _CUSTOM_INSTR

* add custom instructions to evelve_instruction

* format

* fix custom instructions

* fix custom instructions
  • Loading branch information
doichanj authored Jul 18, 2024
1 parent 0388bca commit 91142d7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
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

0 comments on commit 91142d7

Please sign in to comment.