abbreviate long names in qobj_utils

This commit is contained in:
cjwood 2018-12-13 23:47:34 -05:00
parent 0000ee22a0
commit 45e1e05aec
5 changed files with 142 additions and 153 deletions

View File

@ -14,90 +14,56 @@ IS ADDED TO QISKIT TERRA. THEY WILL NOT BE SUPPORTED AFTER THAT.
import copy
import numpy as np
from qiskit.qobj import QobjItem
from qiskit.qobj import QobjInstruction
def qobj_append_item(qobj, exp_index, item):
"""Append a QobjItem to a Qobj experiment.
def append_instr(qobj, exp_index, instruction):
"""Append a QobjInstruction to a QobjExperiment.
Args:
qobj (Qobj): a Qobj object
exp_index (int): The index of the experiment in the qobj
item (QobjItem): The Qobj item to insert
qobj (Qobj): a Qobj object.
exp_index (int): The index of the experiment in the qobj.
instruction (QobjInstruction): instruction to insert.
"""
qobj.experiments[exp_index].instructions.append(item)
qobj.experiments[exp_index].instructions.append(instruction)
return qobj
def qobj_insert_item(qobj, exp_index, item, pos):
"""Insert a QobjItem into a Qobj experiment.
def insert_instr(qobj, exp_index, item, pos):
"""Insert a QobjInstruction into a QobjExperiment.
Args:
qobj (Qobj): a Qobj object
exp_index (int): The index of the experiment in the qobj
item (QobjItem): The Qobj item to insert
exp_index (int): The index of the experiment in the qobj.
instruction(QobjInstruction): instruction to insert.
pos (int): the position to insert the item.
"""
qobj.experiments[exp_index].instructions.insert(pos, item)
return qobj
def qobj_get_item_positions(qobj, exp_index, name):
"""Return all locations of QobjItem in a Qobj experiment.
def get_instr_pos(qobj, exp_index, name):
"""Return all locations of QobjInstruction in a Qobj experiment.
The return list is sorted in reverse order so iterating over it
to insert new items will work as expected.
Args:
qobj (Qobj): a Qobj object
exp_index (int): The index of the experiment in the qobj
name (str): QobjItem name to find
name (str): QobjInstruction name to find
Returns:
list[int]: A list of positions where the QobjItem is located.
list[int]: A list of positions where the QobjInstruction is located.
"""
# Check only the name string of the item
return [i for i, val in enumerate(qobj.experiments[exp_index].instructions)
if val.name == name]
positions = [i for i, val in enumerate(qobj.experiments[exp_index].instructions)
if val.name == name]
return positions
def qobj_get_specific_item_positions(qobj, exp_index, item):
"""Return all locations of QobjItem in a Qobj experiment.
Args:
qobj (Qobj): a Qobj object
exp_index (int): The index of the experiment in the qobj
item (QobjItem): The item to find
Returns:
list[int]: A list of positions where the QobjItem is located.
"""
return [i for i, val in enumerate(qobj.experiments[exp_index].instructions)
if val == item]
def qobj_insert_snapshots_after_barriers(qobj, snapshot):
"""Insert a snapshot instruction after each barrier in qobj.
The label of the input snapshot will be appended with "i" where
"i" ranges from 0 to the 1 - number of barriers.
Args:
snapshot (QobjItem): a snapshot instruction.
Additional Information:
"""
if snapshot.name != "snapshot":
raise ValueError("Invalid snapshot instruction")
label = snapshot.label
for exp_index in range(len(qobj.experiments)):
positions = qobj_get_item_positions(qobj, exp_index, "barrier")
for i, pos in reversed(list(enumerate(positions))):
item = copy.copy(snapshot)
item.label = label + "{}".format(i)
qobj_insert_item(qobj, exp_index, item, pos)
return qobj
def qobj_unitary_item(mat, qubits, label=None):
"""Create a unitary gate qobj item.
def unitary_instr(mat, qubits, label=None):
"""Create a unitary gate QobjInstruction.
Args:
mat (matrix_like): an n-qubit unitary matrix
@ -105,7 +71,7 @@ def qobj_unitary_item(mat, qubits, label=None):
label (str): optional string label for the untiary matrix
Returns:
QobjItem: The qobj item for the unitary instruction.
QobjInstruction: The qobj item for the unitary instruction.
Raises:
ValueError: if the input matrix is not unitary
@ -131,10 +97,38 @@ def qobj_unitary_item(mat, qubits, label=None):
"params": np.array(mat, dtype=complex)}
if label is not None:
instruction["label"] = str(label)
return QobjItem(**instruction)
return QobjInstruction(**instruction)
def qobj_snapshot_item(snapshot_type, label, qubits=None, params=None):
def measure_instr(qubits, memory, registers=None):
"""Create a multi-qubit measure instruction"""
if len(qubits) != len(memory):
raise ValueError("Number of qubits does not match number of memory")
if registers is None:
return QobjInstruction(name='measure', qubits=qubits, memory=memory)
# Case where we also measure to registers
if len(qubits) != len(registers):
raise ValueError("Number of qubits does not match number of registers")
return QobjInstruction(name='measure', qubits=qubits, memory=memory,
register=registers)
def reset_instr(qubits):
"""Create a multi-qubit reset instruction"""
return QobjInstruction(name='reset', qubits=qubits)
def barrier_instr(num_qubits):
"""Create a barrier QobjInstruction."""
return QobjInstruction(name='barrier', qubits=list(range(num_qubits)))
def iden_instr(qubit):
"""Create a barrier QobjInstruction."""
return QobjInstruction(name='id', qubits=[qubit])
def snapshot_instr(snapshot_type, label, qubits=None, params=None):
"""Create a snapshot qobj item.
Args:
@ -145,7 +139,7 @@ def qobj_snapshot_item(snapshot_type, label, qubits=None, params=None):
See additional information.
Returns:
QobjItem: The qobj item for the snapshot instruction.
QobjInstruction: The qobj item for the snapshot instruction.
Additional Information:
@ -184,32 +178,28 @@ def qobj_snapshot_item(snapshot_type, label, qubits=None, params=None):
snap["name"] = "expval_matrix"
snap["params"] = [[1.0, qubits, params]]
# TODO: implicit conversion for Pauli expval params
return QobjItem(**snap)
return QobjInstruction(**snap)
def qobj_measure_item(qubits, memory, registers=None):
"""Create a multi-qubit measure instruction"""
if len(qubits) != len(memory):
raise ValueError("Number of qubits does not match number of memory")
if registers is None:
return QobjItem(name='measure', qubits=qubits, memory=memory)
# Case where we also measure to registers
if len(qubits) != len(registers):
raise ValueError("Number of qubits does not match number of registers")
return QobjItem(name='measure', qubits=qubits, memory=memory,
register=registers)
def insert_snapshots_after_barriers(qobj, snapshot):
"""Insert a snapshot instruction after each barrier in qobj.
The label of the input snapshot will be appended with "i" where
"i" ranges from 0 to the 1 - number of barriers.
def qobj_reset_item(qubits):
"""Create a multi-qubit reset instruction"""
return QobjItem(name='reset', qubits=qubits)
Args:
qobj (Qobj): a qobj to insert snapshots into
snapshot (QobjInstruction): a snapshot instruction.
def qobj_barrier_item(num_qubits):
"""Create a barrier QobjItem."""
return QobjItem(name='barrier', qubits=list(range(num_qubits)))
def qobj_iden_item(qubit):
"""Create a barrier QobjItem."""
return QobjItem(name='id', qubits=[qubit])
Additional Information:
"""
if snapshot.name != "snapshot":
raise ValueError("Invalid snapshot instruction")
label = snapshot.label
for exp_index in range(len(qobj.experiments)):
positions = get_instr_pos(qobj, exp_index, "barrier")
for i, pos in reversed(list(enumerate(positions))):
item = copy.copy(snapshot)
item.label = label + "{}".format(i)
insert_instr(qobj, exp_index, item, pos)
return qobj

View File

@ -13,12 +13,13 @@ import unittest
from test.terra.utils import common
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
from qiskit import compile
from qiskit_aer.noise import NoiseModel, QuantumError
from qiskit_aer.backends import QasmSimulator
from qiskit_aer.noise import NoiseModel
from qiskit_aer.noise.errors.quantum_error import QuantumError
from qiskit_aer.noise.errors.standard_errors import pauli_error
from qiskit_aer.noise.errors.standard_errors import amplitude_damping_error
from qiskit_aer.utils.qobj_utils import qobj_measure_item
from qiskit_aer.utils.qobj_utils import qobj_append_item
from qiskit_aer.utils.qobj_utils import measure_instr
from qiskit_aer.utils.qobj_utils import append_instr
class TestNoise(common.QiskitAerTestCase):
@ -159,8 +160,8 @@ class TestNoise(common.QiskitAerTestCase):
qobj = compile([circuit], backend, shots=shots,
basis_gates=noise_model.basis_gates)
# Add measure to qobj
item = qobj_measure_item([0, 1], [0, 1])
qobj_append_item(qobj, 0, item)
item = measure_instr([0, 1], [0, 1])
append_instr(qobj, 0, item)
# Execute
result = backend.run(qobj, noise_model=noise_model).result()
self.is_completed(result)

View File

@ -13,7 +13,7 @@ import unittest
from test.terra.utils import common
import numpy as np
from qiskit_aer.noise.noiseerror import NoiseError
from qiskit_aer.noise.errors.readout_error import QuantumError
from qiskit_aer.noise.errors.quantum_error import QuantumError
from qiskit_aer.noise.errors.errorutils import standard_gate_unitary

View File

@ -17,9 +17,9 @@ from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit
# direclty by qobj instructions until terra compiler supports them
from qiskit import compile
from qiskit_aer.backends import QasmSimulator
from qiskit_aer.utils.qobj_utils import qobj_insert_item
from qiskit_aer.utils.qobj_utils import qobj_measure_item
from qiskit_aer.utils.qobj_utils import qobj_iden_item
from qiskit_aer.utils.qobj_utils import insert_instr
from qiskit_aer.utils.qobj_utils import measure_instr
from qiskit_aer.utils.qobj_utils import iden_instr
# ==========================================================================
@ -182,9 +182,9 @@ def measure_circuits_qobj_deterministic(allow_sampling=True):
circuit.x(qr[1])
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_measure_item([0, 1], [0, 1]), -1)
insert_instr(qobj, 0, measure_instr([0, 1], [0, 1]), -1)
if not allow_sampling:
qobj_insert_item(qobj, 0, qobj_iden_item(0), -1)
insert_instr(qobj, 0, iden_instr(0), -1)
final_qobj.experiments.append(qobj.experiments[0])
# 3-qubit measure |101>
@ -195,9 +195,9 @@ def measure_circuits_qobj_deterministic(allow_sampling=True):
circuit.x(qr[2])
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_measure_item([0, 1, 2], [0, 1, 2]), -1)
insert_instr(qobj, 0, measure_instr([0, 1, 2], [0, 1, 2]), -1)
if not allow_sampling:
qobj_insert_item(qobj, 0, qobj_iden_item(0), -1)
insert_instr(qobj, 0, iden_instr(0), -1)
final_qobj.experiments.append(qobj.experiments[0])
# 4-qubit measure |1010>
@ -208,9 +208,9 @@ def measure_circuits_qobj_deterministic(allow_sampling=True):
circuit.x(qr[3])
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_measure_item([0, 1, 2, 3], [0, 1, 2, 3]), -1)
insert_instr(qobj, 0, measure_instr([0, 1, 2, 3], [0, 1, 2, 3]), -1)
if not allow_sampling:
qobj_insert_item(qobj, 0, qobj_iden_item(0), -1)
insert_instr(qobj, 0, iden_instr(0), -1)
final_qobj.experiments.append(qobj.experiments[0])
return final_qobj
@ -264,9 +264,9 @@ def measure_circuits_qobj_nondeterministic(allow_sampling=True):
circuit.h(qr[1])
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_measure_item([0, 1], [0, 1]), -1)
insert_instr(qobj, 0, measure_instr([0, 1], [0, 1]), -1)
if not allow_sampling:
qobj_insert_item(qobj, 0, qobj_iden_item(0), -1)
insert_instr(qobj, 0, iden_instr(0), -1)
final_qobj.experiments.append(qobj.experiments[0])
# 3-qubit measure |++0>
@ -277,9 +277,9 @@ def measure_circuits_qobj_nondeterministic(allow_sampling=True):
circuit.h(qr[1])
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_measure_item([0, 1, 2], [0, 1, 2]), -1)
insert_instr(qobj, 0, measure_instr([0, 1, 2], [0, 1, 2]), -1)
if not allow_sampling:
qobj_insert_item(qobj, 0, qobj_iden_item(0), -1)
insert_instr(qobj, 0, iden_instr(0), -1)
final_qobj.experiments.append(qobj.experiments[0])
return final_qobj

View File

@ -12,11 +12,10 @@ Test circuits and reference outputs for measure instruction.
import numpy as np
from qiskit import QuantumRegister, ClassicalRegister, QuantumCircuit, compile
from qiskit_aer.backends import QasmSimulator
from qiskit_aer.utils.qobj_utils import qobj_unitary_item
from qiskit_aer.utils.qobj_utils import qobj_insert_item
from qiskit_aer.utils.qobj_utils import qobj_measure_item
from qiskit_aer.utils.qobj_utils import unitary_instr
from qiskit_aer.utils.qobj_utils import append_instr
from qiskit_aer.utils.qobj_utils import measure_instr
# ==========================================================================
@ -51,64 +50,64 @@ def unitary_gate_circuits_real_deterministic(final_measure=True):
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [0, 1]), -1)
append_instr(qobj, 0, unitary_instr(cx_mat, [0, 1]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
# CX10, |00> state
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [1, 0]), -1)
append_instr(qobj, 0, unitary_instr(cx_mat, [1, 0]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
# CX01.(X^I), |10> state
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(x_mat, [1]), -1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [0, 1]), -1)
append_instr(qobj, 0, unitary_instr(x_mat, [1]))
append_instr(qobj, 0, unitary_instr(cx_mat, [0, 1]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
# CX10.(I^X), |01> state
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(x_mat, [0]), -1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [1, 0]), -1)
append_instr(qobj, 0, unitary_instr(x_mat, [0]))
append_instr(qobj, 0, unitary_instr(cx_mat, [1, 0]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
# CX01.(I^X), |11> state
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(x_mat, [0]), -1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [0, 1]), -1)
append_instr(qobj, 0, unitary_instr(x_mat, [0]))
append_instr(qobj, 0, unitary_instr(cx_mat, [0, 1]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
# CX10.(X^I), |11> state
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(x_mat, [1]), -1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [1, 0]), -1)
append_instr(qobj, 0, unitary_instr(x_mat, [1]))
append_instr(qobj, 0, unitary_instr(cx_mat, [1, 0]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
return final_qobj
@ -217,65 +216,64 @@ def unitary_gate_circuits_complex_deterministic(final_measure=True):
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [0, 1]), -1)
append_instr(qobj, 0, unitary_instr(cx_mat, [0, 1]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
# CX10, |00> state
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [1, 0]), -1)
append_instr(qobj, 0, unitary_instr(cx_mat, [1, 0]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
# CX01.(Y^I), |10> state
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(y_mat, [1]), -1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [0, 1]), -1)
append_instr(qobj, 0, unitary_instr(y_mat, [1]))
append_instr(qobj, 0, unitary_instr(cx_mat, [0, 1]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
# CX10.(I^Y), |01> state
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(y_mat, [0]), -1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [1, 0]), -1)
append_instr(qobj, 0, unitary_instr(y_mat, [0]))
append_instr(qobj, 0, unitary_instr(cx_mat, [1, 0]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
# CX01.(I^Y), |11> state
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(y_mat, [0]), -1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [0, 1]), -1)
append_instr(qobj, 0, unitary_instr(y_mat, [0]))
append_instr(qobj, 0, unitary_instr(cx_mat, [0, 1]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
# CX10.(Y^I), |11> state
circuit = QuantumCircuit(*regs)
circuit.barrier(qr)
qobj = compile(circuit, QasmSimulator(), shots=1)
qobj_insert_item(qobj, 0, qobj_unitary_item(y_mat, [1]), -1)
qobj_insert_item(qobj, 0, qobj_unitary_item(cx_mat, [1, 0]), -1)
append_instr(qobj, 0, unitary_instr(y_mat, [1]))
append_instr(qobj, 0, unitary_instr(cx_mat, [1, 0]))
if final_measure:
qobj_insert_item(qobj, 0, qobj_measure_item([0], [0]), -1)
qobj_insert_item(qobj, 0, qobj_measure_item([1], [1]), -1)
append_instr(qobj, 0, measure_instr([0], [0]))
append_instr(qobj, 0, measure_instr([1], [1]))
final_qobj.experiments.append(qobj.experiments[0])
return final_qobj