_id
stringlengths
2
7
title
stringlengths
1
88
partition
stringclasses
3 values
text
stringlengths
75
19.8k
language
stringclasses
1 value
meta_information
dict
q268300
CouplingMap.add_edge
test
def add_edge(self, src, dst): """ Add directed edge to coupling graph. src (int): source physical qubit dst (int): destination physical qubit """ if src not in self.physical_qubits: self.add_physical_qubit(src) if dst not in self.physical_qubits: self.add_physical_qubit(dst) self.graph.add_edge(src, dst) self._dist_matrix = None
python
{ "resource": "" }
q268301
CouplingMap.subgraph
test
def subgraph(self, nodelist): """Return a CouplingMap object for a subgraph of self. nodelist (list): list of integer node labels """ subcoupling = CouplingMap() subcoupling.graph = self.graph.subgraph(nodelist) for node in nodelist: if node not in subcoupling.physical_qubits: subcoupling.add_physical_qubit(node) return subcoupling
python
{ "resource": "" }
q268302
CouplingMap.physical_qubits
test
def physical_qubits(self): """Returns a sorted list of physical_qubits""" if self._qubit_list is None: self._qubit_list = sorted([pqubit for pqubit in self.graph.nodes]) return self._qubit_list
python
{ "resource": "" }
q268303
CouplingMap.is_connected
test
def is_connected(self): """ Test if the graph is connected. Return True if connected, False otherwise """ try: return nx.is_weakly_connected(self.graph) except nx.exception.NetworkXException: return False
python
{ "resource": "" }
q268304
CouplingMap._compute_distance_matrix
test
def _compute_distance_matrix(self): """Compute the full distance matrix on pairs of nodes. The distance map self._dist_matrix is computed from the graph using all_pairs_shortest_path_length. """ if not self.is_connected(): raise CouplingError("coupling graph not connected") lengths = nx.all_pairs_shortest_path_length(self.graph.to_undirected(as_view=True)) lengths = dict(lengths) size = len(lengths) cmap = np.zeros((size, size)) for idx in range(size): cmap[idx, np.fromiter(lengths[idx].keys(), dtype=int)] = np.fromiter( lengths[idx].values(), dtype=int) self._dist_matrix = cmap
python
{ "resource": "" }
q268305
CouplingMap.distance
test
def distance(self, physical_qubit1, physical_qubit2): """Returns the undirected distance between physical_qubit1 and physical_qubit2. Args: physical_qubit1 (int): A physical qubit physical_qubit2 (int): Another physical qubit Returns: int: The undirected distance Raises: CouplingError: if the qubits do not exist in the CouplingMap """ if physical_qubit1 not in self.physical_qubits: raise CouplingError("%s not in coupling graph" % (physical_qubit1,)) if physical_qubit2 not in self.physical_qubits: raise CouplingError("%s not in coupling graph" % (physical_qubit2,)) if self._dist_matrix is None: self._compute_distance_matrix() return self._dist_matrix[physical_qubit1, physical_qubit2]
python
{ "resource": "" }
q268306
transpile
test
def transpile(circuits, backend=None, basis_gates=None, coupling_map=None, initial_layout=None, seed_mapper=None, pass_manager=None): """transpile one or more circuits. Args: circuits (QuantumCircuit or list[QuantumCircuit]): circuits to compile backend (BaseBackend): a backend to compile for basis_gates (list[str]): list of basis gate names supported by the target. Default: ['u1','u2','u3','cx','id'] coupling_map (list): coupling map (perhaps custom) to target in mapping initial_layout (Layout or dict or list): Initial position of virtual qubits on physical qubits. The final layout is not guaranteed to be the same, as the transpiler may permute qubits through swaps or other means. seed_mapper (int): random seed for the swap_mapper pass_manager (PassManager): a pass_manager for the transpiler stages Returns: QuantumCircuit or list[QuantumCircuit]: transpiled circuit(s). Raises: TranspilerError: in case of bad inputs to transpiler or errors in passes """ warnings.warn("qiskit.transpiler.transpile() has been deprecated and will be " "removed in the 0.9 release. Use qiskit.compiler.transpile() instead.", DeprecationWarning) return compiler.transpile(circuits=circuits, backend=backend, basis_gates=basis_gates, coupling_map=coupling_map, initial_layout=initial_layout, seed_transpiler=seed_mapper, pass_manager=pass_manager)
python
{ "resource": "" }
q268307
cu1
test
def cu1(self, theta, ctl, tgt): """Apply cu1 from ctl to tgt with angle theta.""" return self.append(Cu1Gate(theta), [ctl, tgt], [])
python
{ "resource": "" }
q268308
InstructionSet.inverse
test
def inverse(self): """Invert all instructions.""" for index, instruction in enumerate(self.instructions): self.instructions[index] = instruction.inverse() return self
python
{ "resource": "" }
q268309
InstructionSet.q_if
test
def q_if(self, *qregs): """Add controls to all instructions.""" for gate in self.instructions: gate.q_if(*qregs) return self
python
{ "resource": "" }
q268310
InstructionSet.c_if
test
def c_if(self, classical, val): """Add classical control register to all instructions.""" for gate in self.instructions: gate.c_if(classical, val) return self
python
{ "resource": "" }
q268311
_Broker.subscribe
test
def subscribe(self, event, callback): """Subscribes to an event, so when it's emitted all the callbacks subscribed, will be executed. We are not allowing double registration. Args event (string): The event to subscribed in the form of: "terra.<component>.<method>.<action>" callback (callable): The callback that will be executed when an event is emitted. """ if not callable(callback): raise QiskitError("Callback is not a callable!") if event not in self._subscribers: self._subscribers[event] = [] new_subscription = self._Subscription(event, callback) if new_subscription in self._subscribers[event]: # We are not allowing double subscription return False self._subscribers[event].append(new_subscription) return True
python
{ "resource": "" }
q268312
_Broker.dispatch
test
def dispatch(self, event, *args, **kwargs): """Emits an event if there are any subscribers. Args event (String): The event to be emitted args: Arguments linked with the event kwargs: Named arguments linked with the event """ # No event, no subscribers. if event not in self._subscribers: return for subscriber in self._subscribers[event]: subscriber.callback(*args, **kwargs)
python
{ "resource": "" }
q268313
_Broker.unsubscribe
test
def unsubscribe(self, event, callback): """ Unsubscribe the specific callback to the event. Args event (String): The event to unsubscribe callback (callable): The callback that won't be executed anymore Returns True: if we have successfully unsubscribed to the event False: if there's no callback previously registered """ try: self._subscribers[event].remove(self._Subscription(event, callback)) except KeyError: return False return True
python
{ "resource": "" }
q268314
Publisher.publish
test
def publish(self, event, *args, **kwargs): """ Triggers an event, and associates some data to it, so if there are any subscribers, their callback will be called synchronously. """ return self._broker.dispatch(event, *args, **kwargs)
python
{ "resource": "" }
q268315
initialize
test
def initialize(self, params, qubits): """Apply initialize to circuit.""" if isinstance(qubits, QuantumRegister): qubits = qubits[:] else: qubits = _convert_to_bits([qubits], [qbit for qreg in self.qregs for qbit in qreg])[0] return self.append(Initialize(params), qubits)
python
{ "resource": "" }
q268316
Initialize._define
test
def _define(self): """Calculate a subcircuit that implements this initialization Implements a recursive initialization algorithm, including optimizations, from "Synthesis of Quantum Logic Circuits" Shende, Bullock, Markov https://arxiv.org/abs/quant-ph/0406176v5 Additionally implements some extra optimizations: remove zero rotations and double cnots. """ # call to generate the circuit that takes the desired vector to zero disentangling_circuit = self.gates_to_uncompute() # invert the circuit to create the desired vector from zero (assuming # the qubits are in the zero state) initialize_instr = disentangling_circuit.to_instruction().inverse() q = QuantumRegister(self.num_qubits, 'q') initialize_circuit = QuantumCircuit(q, name='init_def') for qubit in q: initialize_circuit.append(Reset(), [qubit]) initialize_circuit.append(initialize_instr, q[:]) self.definition = initialize_circuit.data
python
{ "resource": "" }
q268317
Initialize.gates_to_uncompute
test
def gates_to_uncompute(self): """ Call to create a circuit with gates that take the desired vector to zero. Returns: QuantumCircuit: circuit to take self.params vector to |00..0> """ q = QuantumRegister(self.num_qubits) circuit = QuantumCircuit(q, name='disentangler') # kick start the peeling loop, and disentangle one-by-one from LSB to MSB remaining_param = self.params for i in range(self.num_qubits): # work out which rotations must be done to disentangle the LSB # qubit (we peel away one qubit at a time) (remaining_param, thetas, phis) = Initialize._rotations_to_disentangle(remaining_param) # perform the required rotations to decouple the LSB qubit (so that # it can be "factored" out, leaving a shorter amplitude vector to peel away) rz_mult = self._multiplex(RZGate, phis) ry_mult = self._multiplex(RYGate, thetas) circuit.append(rz_mult.to_instruction(), q[i:self.num_qubits]) circuit.append(ry_mult.to_instruction(), q[i:self.num_qubits]) return circuit
python
{ "resource": "" }
q268318
Initialize._bloch_angles
test
def _bloch_angles(pair_of_complex): """ Static internal method to work out rotation to create the passed in qubit from the zero vector. """ [a_complex, b_complex] = pair_of_complex # Force a and b to be complex, as otherwise numpy.angle might fail. a_complex = complex(a_complex) b_complex = complex(b_complex) mag_a = np.absolute(a_complex) final_r = float(np.sqrt(mag_a ** 2 + np.absolute(b_complex) ** 2)) if final_r < _EPS: theta = 0 phi = 0 final_r = 0 final_t = 0 else: theta = float(2 * np.arccos(mag_a / final_r)) a_arg = np.angle(a_complex) b_arg = np.angle(b_complex) final_t = a_arg + b_arg phi = b_arg - a_arg return final_r * np.exp(1.J * final_t / 2), theta, phi
python
{ "resource": "" }
q268319
Initialize._multiplex
test
def _multiplex(self, target_gate, list_of_angles): """ Return a recursive implementation of a multiplexor circuit, where each instruction itself has a decomposition based on smaller multiplexors. The LSB is the multiplexor "data" and the other bits are multiplexor "select". Args: target_gate (Gate): Ry or Rz gate to apply to target qubit, multiplexed over all other "select" qubits list_of_angles (list[float]): list of rotation angles to apply Ry and Rz Returns: DAGCircuit: the circuit implementing the multiplexor's action """ list_len = len(list_of_angles) local_num_qubits = int(math.log2(list_len)) + 1 q = QuantumRegister(local_num_qubits) circuit = QuantumCircuit(q, name="multiplex" + local_num_qubits.__str__()) lsb = q[0] msb = q[local_num_qubits - 1] # case of no multiplexing: base case for recursion if local_num_qubits == 1: circuit.append(target_gate(list_of_angles[0]), [q[0]]) return circuit # calc angle weights, assuming recursion (that is the lower-level # requested angles have been correctly implemented by recursion angle_weight = scipy.kron([[0.5, 0.5], [0.5, -0.5]], np.identity(2 ** (local_num_qubits - 2))) # calc the combo angles list_of_angles = angle_weight.dot(np.array(list_of_angles)).tolist() # recursive step on half the angles fulfilling the above assumption multiplex_1 = self._multiplex(target_gate, list_of_angles[0:(list_len // 2)]) circuit.append(multiplex_1.to_instruction(), q[0:-1]) # attach CNOT as follows, thereby flipping the LSB qubit circuit.append(CnotGate(), [msb, lsb]) # implement extra efficiency from the paper of cancelling adjacent # CNOTs (by leaving out last CNOT and reversing (NOT inverting) the # second lower-level multiplex) multiplex_2 = self._multiplex(target_gate, list_of_angles[(list_len // 2):]) if list_len > 1: circuit.append(multiplex_2.to_instruction().mirror(), q[0:-1]) else: circuit.append(multiplex_2.to_instruction(), q[0:-1]) # attach a final CNOT circuit.append(CnotGate(), [msb, lsb]) return circuit
python
{ "resource": "" }
q268320
Layout.is_virtual
test
def is_virtual(value): """Checks if value has the format of a virtual qubit """ return value is None or isinstance(value, tuple) and len(value) == 2 and isinstance( value[0], Register) and isinstance(value[1], int)
python
{ "resource": "" }
q268321
Layout.copy
test
def copy(self): """Returns a copy of a Layout instance.""" layout_copy = type(self)() layout_copy._p2v = self._p2v.copy() layout_copy._v2p = self._v2p.copy() return layout_copy
python
{ "resource": "" }
q268322
Layout.combine_into_edge_map
test
def combine_into_edge_map(self, another_layout): """Combines self and another_layout into an "edge map". For example:: self another_layout resulting edge map qr_1 -> 0 0 <- q_2 qr_1 -> q_2 qr_2 -> 2 2 <- q_1 qr_2 -> q_1 qr_3 -> 3 3 <- q_0 qr_3 -> q_0 The edge map is used to compose dags via, for example, compose_back. Args: another_layout (Layout): The other layout to combine. Returns: dict: A "edge map". Raises: LayoutError: another_layout can be bigger than self, but not smaller. Otherwise, raises. """ edge_map = dict() for virtual, physical in self.get_virtual_bits().items(): if physical not in another_layout._p2v: raise LayoutError('The wire_map_from_layouts() method does not support when the' ' other layout (another_layout) is smaller.') edge_map[virtual] = another_layout[physical] return edge_map
python
{ "resource": "" }
q268323
ccx
test
def ccx(self, ctl1, ctl2, tgt): """Apply Toffoli to from ctl1 and ctl2 to tgt.""" return self.append(ToffoliGate(), [ctl1, ctl2, tgt], [])
python
{ "resource": "" }
q268324
Instruction.insert
test
def insert(self, start_time: int, schedule: ScheduleComponent) -> 'ScheduleComponent': """Return a new schedule with `schedule` inserted within `self` at `start_time`. Args: start_time: time to be inserted schedule: schedule to be inserted """ return ops.insert(self, start_time, schedule)
python
{ "resource": "" }
q268325
FencedObject._check_if_fenced
test
def _check_if_fenced(self, name): """ Checks if the attribute name is in the list of attributes to protect. If so, raises TranspilerAccessError. Args: name (string): the attribute name to check Raises: TranspilerAccessError: when name is the list of attributes to protect. """ if name in object.__getattribute__(self, '_attributes_to_fence'): raise TranspilerAccessError("The fenced %s has the property %s protected" % (type(object.__getattribute__(self, '_wrapped')), name))
python
{ "resource": "" }
q268326
gates_to_idx
test
def gates_to_idx(gates, qregs): """Converts gate tuples into a nested list of integers. Args: gates (list): List of (QuantumRegister, int) pairs representing gates. qregs (dict): List of )QuantumRegister, int) tuples. Returns: list: Nested list of integers for gates. """ sizes = [qr.size for qr in qregs.values()] reg_idx = np.cumsum([0]+sizes) regint = {} for ind, qreg in enumerate(qregs.values()): regint[qreg] = ind out = np.zeros(2*len(gates), dtype=np.int32) for idx, gate in enumerate(gates): out[2*idx] = reg_idx[regint[gate[0][0]]]+gate[0][1] out[2*idx+1] = reg_idx[regint[gate[1][0]]]+gate[1][1] return out
python
{ "resource": "" }
q268327
StochasticSwap.run
test
def run(self, dag): """ Run the StochasticSwap pass on `dag`. Args: dag (DAGCircuit): DAG to map. Returns: DAGCircuit: A mapped DAG. Raises: TranspilerError: if the coupling map or the layout are not compatible with the DAG """ if self.initial_layout is None: if self.property_set["layout"]: self.initial_layout = self.property_set["layout"] else: self.initial_layout = Layout.generate_trivial_layout(*dag.qregs.values()) if len(dag.qubits()) != len(self.initial_layout): raise TranspilerError('The layout does not match the amount of qubits in the DAG') if len(self.coupling_map.physical_qubits) != len(self.initial_layout): raise TranspilerError( "Mappers require to have the layout to be the same size as the coupling map") self.input_layout = self.initial_layout.copy() self.qregs = dag.qregs if self.seed is None: self.seed = np.random.randint(0, np.iinfo(np.int32).max) self.rng = np.random.RandomState(self.seed) logger.debug("StochasticSwap RandomState seeded with seed=%s", self.seed) new_dag = self._mapper(dag, self.coupling_map, trials=self.trials) # self.property_set["layout"] = self.initial_layout return new_dag
python
{ "resource": "" }
q268328
StochasticSwap._layer_update
test
def _layer_update(self, i, first_layer, best_layout, best_depth, best_circuit, layer_list): """Provide a DAGCircuit for a new mapped layer. i (int) = layer number first_layer (bool) = True if this is the first layer in the circuit with any multi-qubit gates best_layout (Layout) = layout returned from _layer_permutation best_depth (int) = depth returned from _layer_permutation best_circuit (DAGCircuit) = swap circuit returned from _layer_permutation layer_list (list) = list of DAGCircuit objects for each layer, output of DAGCircuit layers() method Return a DAGCircuit object to append to the output DAGCircuit that the _mapper method is building. """ layout = best_layout logger.debug("layer_update: layout = %s", pformat(layout)) logger.debug("layer_update: self.initial_layout = %s", pformat(self.initial_layout)) dagcircuit_output = DAGCircuit() for register in layout.get_virtual_bits().keys(): if register[0] not in dagcircuit_output.qregs.values(): dagcircuit_output.add_qreg(register[0]) # If this is the first layer with multi-qubit gates, # output all layers up to this point and ignore any # swap gates. Set the initial layout. if first_layer: logger.debug("layer_update: first multi-qubit gate layer") # Output all layers up to this point for j in range(i + 1): # Make qubit edge map and extend by classical bits edge_map = layout.combine_into_edge_map(self.initial_layout) for bit in dagcircuit_output.clbits(): edge_map[bit] = bit dagcircuit_output.compose_back(layer_list[j]["graph"], edge_map) # Otherwise, we output the current layer and the associated swap gates. else: # Output any swaps if best_depth > 0: logger.debug("layer_update: there are swaps in this layer, " "depth %d", best_depth) dagcircuit_output.extend_back(best_circuit) else: logger.debug("layer_update: there are no swaps in this layer") # Make qubit edge map and extend by classical bits edge_map = layout.combine_into_edge_map(self.initial_layout) for bit in dagcircuit_output.clbits(): edge_map[bit] = bit # Output this layer dagcircuit_output.compose_back(layer_list[i]["graph"], edge_map) return dagcircuit_output
python
{ "resource": "" }
q268329
pauli_group
test
def pauli_group(number_of_qubits, case='weight'): """Return the Pauli group with 4^n elements. The phases have been removed. case 'weight' is ordered by Pauli weights and case 'tensor' is ordered by I,X,Y,Z counting lowest qubit fastest. Args: number_of_qubits (int): number of qubits case (str): determines ordering of group elements ('weight' or 'tensor') Returns: list: list of Pauli objects Raises: QiskitError: case is not 'weight' or 'tensor' QiskitError: number_of_qubits is larger than 4 """ if number_of_qubits < 5: temp_set = [] if case == 'weight': tmp = pauli_group(number_of_qubits, case='tensor') # sort on the weight of the Pauli operator return sorted(tmp, key=lambda x: -np.count_nonzero( np.array(x.to_label(), 'c') == b'I')) elif case == 'tensor': # the Pauli set is in tensor order II IX IY IZ XI ... for k in range(4 ** number_of_qubits): z = np.zeros(number_of_qubits, dtype=np.bool) x = np.zeros(number_of_qubits, dtype=np.bool) # looping over all the qubits for j in range(number_of_qubits): # making the Pauli for each j fill it in from the # end first element = (k // (4 ** j)) % 4 if element == 1: x[j] = True elif element == 2: z[j] = True x[j] = True elif element == 3: z[j] = True temp_set.append(Pauli(z, x)) return temp_set else: raise QiskitError("Only support 'weight' or 'tensor' cases " "but you have {}.".format(case)) raise QiskitError("Only support number of qubits is less than 5")
python
{ "resource": "" }
q268330
Pauli.from_label
test
def from_label(cls, label): r"""Take pauli string to construct pauli. The qubit index of pauli label is q_{n-1} ... q_0. E.g., a pauli is $P_{n-1} \otimes ... \otimes P_0$ Args: label (str): pauli label Returns: Pauli: the constructed pauli Raises: QiskitError: invalid character in the label """ z = np.zeros(len(label), dtype=np.bool) x = np.zeros(len(label), dtype=np.bool) for i, char in enumerate(label): if char == 'X': x[-i - 1] = True elif char == 'Z': z[-i - 1] = True elif char == 'Y': z[-i - 1] = True x[-i - 1] = True elif char != 'I': raise QiskitError("Pauli string must be only consisted of 'I', 'X', " "'Y' or 'Z' but you have {}.".format(char)) return cls(z=z, x=x)
python
{ "resource": "" }
q268331
Pauli._init_from_bool
test
def _init_from_bool(self, z, x): """Construct pauli from boolean array. Args: z (numpy.ndarray): boolean, z vector x (numpy.ndarray): boolean, x vector Returns: Pauli: self Raises: QiskitError: if z or x are None or the length of z and x are different. """ if z is None: raise QiskitError("z vector must not be None.") if x is None: raise QiskitError("x vector must not be None.") if len(z) != len(x): raise QiskitError("length of z and x vectors must be " "the same. (z: {} vs x: {})".format(len(z), len(x))) z = _make_np_bool(z) x = _make_np_bool(x) self._z = z self._x = x return self
python
{ "resource": "" }
q268332
Pauli.sgn_prod
test
def sgn_prod(p1, p2): r""" Multiply two Paulis and track the phase. $P_3 = P_1 \otimes P_2$: X*Y Args: p1 (Pauli): pauli 1 p2 (Pauli): pauli 2 Returns: Pauli: the multiplied pauli complex: the sign of the multiplication, 1, -1, 1j or -1j """ phase = Pauli._prod_phase(p1, p2) new_pauli = p1 * p2 return new_pauli, phase
python
{ "resource": "" }
q268333
Pauli.to_operator
test
def to_operator(self): """Convert to Operator object.""" # Place import here to avoid cyclic import from circuit visualization from qiskit.quantum_info.operators.operator import Operator return Operator(self.to_matrix())
python
{ "resource": "" }
q268334
Pauli.to_instruction
test
def to_instruction(self): """Convert to Pauli circuit instruction.""" from qiskit.circuit import QuantumCircuit, QuantumRegister from qiskit.extensions.standard import IdGate, XGate, YGate, ZGate gates = {'I': IdGate(), 'X': XGate(), 'Y': YGate(), 'Z': ZGate()} label = self.to_label() n_qubits = self.numberofqubits qreg = QuantumRegister(n_qubits) circuit = QuantumCircuit(qreg, name='Pauli:{}'.format(label)) for i, pauli in enumerate(reversed(label)): circuit.append(gates[pauli], [qreg[i]]) return circuit.to_instruction()
python
{ "resource": "" }
q268335
Pauli.update_z
test
def update_z(self, z, indices=None): """ Update partial or entire z. Args: z (numpy.ndarray or list): to-be-updated z indices (numpy.ndarray or list or optional): to-be-updated qubit indices Returns: Pauli: self Raises: QiskitError: when updating whole z, the number of qubits must be the same. """ z = _make_np_bool(z) if indices is None: if len(self._z) != len(z): raise QiskitError("During updating whole z, you can not " "change the number of qubits.") self._z = z else: if not isinstance(indices, list) and not isinstance(indices, np.ndarray): indices = [indices] for p, idx in enumerate(indices): self._z[idx] = z[p] return self
python
{ "resource": "" }
q268336
Pauli.update_x
test
def update_x(self, x, indices=None): """ Update partial or entire x. Args: x (numpy.ndarray or list): to-be-updated x indices (numpy.ndarray or list or optional): to-be-updated qubit indices Returns: Pauli: self Raises: QiskitError: when updating whole x, the number of qubits must be the same. """ x = _make_np_bool(x) if indices is None: if len(self._x) != len(x): raise QiskitError("During updating whole x, you can not change " "the number of qubits.") self._x = x else: if not isinstance(indices, list) and not isinstance(indices, np.ndarray): indices = [indices] for p, idx in enumerate(indices): self._x[idx] = x[p] return self
python
{ "resource": "" }
q268337
Pauli.insert_paulis
test
def insert_paulis(self, indices=None, paulis=None, pauli_labels=None): """ Insert or append pauli to the targeted indices. If indices is None, it means append at the end. Args: indices (list[int]): the qubit indices to be inserted paulis (Pauli): the to-be-inserted or appended pauli pauli_labels (list[str]): the to-be-inserted or appended pauli label Note: the indices refers to the localion of original paulis, e.g. if indices = [0, 2], pauli_labels = ['Z', 'I'] and original pauli = 'ZYXI' the pauli will be updated to ZY'I'XI'Z' 'Z' and 'I' are inserted before the qubit at 0 and 2. Returns: Pauli: self Raises: QiskitError: provide both `paulis` and `pauli_labels` at the same time """ if pauli_labels is not None: if paulis is not None: raise QiskitError("Please only provide either `paulis` or `pauli_labels`") if isinstance(pauli_labels, str): pauli_labels = list(pauli_labels) # since pauli label is in reversed order. paulis = Pauli.from_label(pauli_labels[::-1]) if indices is None: # append self._z = np.concatenate((self._z, paulis.z)) self._x = np.concatenate((self._x, paulis.x)) else: if not isinstance(indices, list): indices = [indices] self._z = np.insert(self._z, indices, paulis.z) self._x = np.insert(self._x, indices, paulis.x) return self
python
{ "resource": "" }
q268338
Pauli.append_paulis
test
def append_paulis(self, paulis=None, pauli_labels=None): """ Append pauli at the end. Args: paulis (Pauli): the to-be-inserted or appended pauli pauli_labels (list[str]): the to-be-inserted or appended pauli label Returns: Pauli: self """ return self.insert_paulis(None, paulis=paulis, pauli_labels=pauli_labels)
python
{ "resource": "" }
q268339
Pauli.delete_qubits
test
def delete_qubits(self, indices): """ Delete pauli at the indices. Args: indices(list[int]): the indices of to-be-deleted paulis Returns: Pauli: self """ if not isinstance(indices, list): indices = [indices] self._z = np.delete(self._z, indices) self._x = np.delete(self._x, indices) return self
python
{ "resource": "" }
q268340
Pauli.random
test
def random(cls, num_qubits, seed=None): """Return a random Pauli on number of qubits. Args: num_qubits (int): the number of qubits seed (int): Optional. To set a random seed. Returns: Pauli: the random pauli """ if seed is not None: np.random.seed(seed) z = np.random.randint(2, size=num_qubits).astype(np.bool) x = np.random.randint(2, size=num_qubits).astype(np.bool) return cls(z, x)
python
{ "resource": "" }
q268341
Pauli.pauli_single
test
def pauli_single(cls, num_qubits, index, pauli_label): """ Generate single qubit pauli at index with pauli_label with length num_qubits. Args: num_qubits (int): the length of pauli index (int): the qubit index to insert the single qubii pauli_label (str): pauli Returns: Pauli: single qubit pauli """ tmp = Pauli.from_label(pauli_label) z = np.zeros(num_qubits, dtype=np.bool) x = np.zeros(num_qubits, dtype=np.bool) z[index] = tmp.z[0] x[index] = tmp.x[0] return cls(z, x)
python
{ "resource": "" }
q268342
QasmSimulatorPy._get_measure_outcome
test
def _get_measure_outcome(self, qubit): """Simulate the outcome of measurement of a qubit. Args: qubit (int): the qubit to measure Return: tuple: pair (outcome, probability) where outcome is '0' or '1' and probability is the probability of the returned outcome. """ # Axis for numpy.sum to compute probabilities axis = list(range(self._number_of_qubits)) axis.remove(self._number_of_qubits - 1 - qubit) probabilities = np.sum(np.abs(self._statevector) ** 2, axis=tuple(axis)) # Compute einsum index string for 1-qubit matrix multiplication random_number = self._local_random.rand() if random_number < probabilities[0]: return '0', probabilities[0] # Else outcome was '1' return '1', probabilities[1]
python
{ "resource": "" }
q268343
QasmSimulatorPy._add_sample_measure
test
def _add_sample_measure(self, measure_params, num_samples): """Generate memory samples from current statevector. Args: measure_params (list): List of (qubit, cmembit) values for measure instructions to sample. num_samples (int): The number of memory samples to generate. Returns: list: A list of memory values in hex format. """ # Get unique qubits that are actually measured measured_qubits = list({qubit for qubit, cmembit in measure_params}) num_measured = len(measured_qubits) # Axis for numpy.sum to compute probabilities axis = list(range(self._number_of_qubits)) for qubit in reversed(measured_qubits): # Remove from largest qubit to smallest so list position is correct # with respect to position from end of the list axis.remove(self._number_of_qubits - 1 - qubit) probabilities = np.reshape(np.sum(np.abs(self._statevector) ** 2, axis=tuple(axis)), 2 ** num_measured) # Generate samples on measured qubits samples = self._local_random.choice(range(2 ** num_measured), num_samples, p=probabilities) # Convert to bit-strings memory = [] for sample in samples: classical_memory = self._classical_memory for count, (qubit, cmembit) in enumerate(sorted(measure_params)): qubit_outcome = int((sample & (1 << count)) >> count) membit = 1 << cmembit classical_memory = (classical_memory & (~membit)) | (qubit_outcome << cmembit) value = bin(classical_memory)[2:] memory.append(hex(int(value, 2))) return memory
python
{ "resource": "" }
q268344
QasmSimulatorPy._add_qasm_measure
test
def _add_qasm_measure(self, qubit, cmembit, cregbit=None): """Apply a measure instruction to a qubit. Args: qubit (int): qubit is the qubit measured. cmembit (int): is the classical memory bit to store outcome in. cregbit (int, optional): is the classical register bit to store outcome in. """ # get measure outcome outcome, probability = self._get_measure_outcome(qubit) # update classical state membit = 1 << cmembit self._classical_memory = (self._classical_memory & (~membit)) | (int(outcome) << cmembit) if cregbit is not None: regbit = 1 << cregbit self._classical_register = \ (self._classical_register & (~regbit)) | (int(outcome) << cregbit) # update quantum state if outcome == '0': update_diag = [[1 / np.sqrt(probability), 0], [0, 0]] else: update_diag = [[0, 0], [0, 1 / np.sqrt(probability)]] # update classical state self._add_unitary_single(update_diag, qubit)
python
{ "resource": "" }
q268345
QasmSimulatorPy._add_qasm_reset
test
def _add_qasm_reset(self, qubit): """Apply a reset instruction to a qubit. Args: qubit (int): the qubit being rest This is done by doing a simulating a measurement outcome and projecting onto the outcome state while renormalizing. """ # get measure outcome outcome, probability = self._get_measure_outcome(qubit) # update quantum state if outcome == '0': update = [[1 / np.sqrt(probability), 0], [0, 0]] self._add_unitary_single(update, qubit) else: update = [[0, 1 / np.sqrt(probability)], [0, 0]] self._add_unitary_single(update, qubit)
python
{ "resource": "" }
q268346
QasmSimulatorPy._validate_initial_statevector
test
def _validate_initial_statevector(self): """Validate an initial statevector""" # If initial statevector isn't set we don't need to validate if self._initial_statevector is None: return # Check statevector is correct length for number of qubits length = len(self._initial_statevector) required_dim = 2 ** self._number_of_qubits if length != required_dim: raise BasicAerError('initial statevector is incorrect length: ' + '{} != {}'.format(length, required_dim))
python
{ "resource": "" }
q268347
QasmSimulatorPy._initialize_statevector
test
def _initialize_statevector(self): """Set the initial statevector for simulation""" if self._initial_statevector is None: # Set to default state of all qubits in |0> self._statevector = np.zeros(2 ** self._number_of_qubits, dtype=complex) self._statevector[0] = 1 else: self._statevector = self._initial_statevector.copy() # Reshape to rank-N tensor self._statevector = np.reshape(self._statevector, self._number_of_qubits * [2])
python
{ "resource": "" }
q268348
QasmSimulatorPy._get_statevector
test
def _get_statevector(self): """Return the current statevector in JSON Result spec format""" vec = np.reshape(self._statevector, 2 ** self._number_of_qubits) # Expand complex numbers vec = np.stack([vec.real, vec.imag], axis=1) # Truncate small values vec[abs(vec) < self._chop_threshold] = 0.0 return vec
python
{ "resource": "" }
q268349
QasmSimulatorPy._validate_measure_sampling
test
def _validate_measure_sampling(self, experiment): """Determine if measure sampling is allowed for an experiment Args: experiment (QobjExperiment): a qobj experiment. """ # If shots=1 we should disable measure sampling. # This is also required for statevector simulator to return the # correct final statevector without silently dropping final measurements. if self._shots <= 1: self._sample_measure = False return # Check for config flag if hasattr(experiment.config, 'allows_measure_sampling'): self._sample_measure = experiment.config.allows_measure_sampling # If flag isn't found do a simple test to see if a circuit contains # no reset instructions, and no gates instructions after # the first measure. else: measure_flag = False for instruction in experiment.instructions: # If circuit contains reset operations we cannot sample if instruction.name == "reset": self._sample_measure = False return # If circuit contains a measure option then we can # sample only if all following operations are measures if measure_flag: # If we find a non-measure instruction # we cannot do measure sampling if instruction.name not in ["measure", "barrier", "id", "u0"]: self._sample_measure = False return elif instruction.name == "measure": measure_flag = True # If we made it to the end of the circuit without returning # measure sampling is allowed self._sample_measure = True
python
{ "resource": "" }
q268350
QasmSimulatorPy.run
test
def run(self, qobj, backend_options=None): """Run qobj asynchronously. Args: qobj (Qobj): payload of the experiment backend_options (dict): backend options Returns: BasicAerJob: derived from BaseJob Additional Information: backend_options: Is a dict of options for the backend. It may contain * "initial_statevector": vector_like The "initial_statevector" option specifies a custom initial initial statevector for the simulator to be used instead of the all zero state. This size of this vector must be correct for the number of qubits in all experiments in the qobj. Example:: backend_options = { "initial_statevector": np.array([1, 0, 0, 1j]) / np.sqrt(2), } """ self._set_options(qobj_config=qobj.config, backend_options=backend_options) job_id = str(uuid.uuid4()) job = BasicAerJob(self, job_id, self._run_job, qobj) job.submit() return job
python
{ "resource": "" }
q268351
QasmSimulatorPy._run_job
test
def _run_job(self, job_id, qobj): """Run experiments in qobj Args: job_id (str): unique id for the job. qobj (Qobj): job description Returns: Result: Result object """ self._validate(qobj) result_list = [] self._shots = qobj.config.shots self._memory = getattr(qobj.config, 'memory', False) self._qobj_config = qobj.config start = time.time() for experiment in qobj.experiments: result_list.append(self.run_experiment(experiment)) end = time.time() result = {'backend_name': self.name(), 'backend_version': self._configuration.backend_version, 'qobj_id': qobj.qobj_id, 'job_id': job_id, 'results': result_list, 'status': 'COMPLETED', 'success': True, 'time_taken': (end - start), 'header': qobj.header.as_dict()} return Result.from_dict(result)
python
{ "resource": "" }
q268352
QasmSimulatorPy._validate
test
def _validate(self, qobj): """Semantic validations of the qobj which cannot be done via schemas.""" n_qubits = qobj.config.n_qubits max_qubits = self.configuration().n_qubits if n_qubits > max_qubits: raise BasicAerError('Number of qubits {} '.format(n_qubits) + 'is greater than maximum ({}) '.format(max_qubits) + 'for "{}".'.format(self.name())) for experiment in qobj.experiments: name = experiment.header.name if experiment.config.memory_slots == 0: logger.warning('No classical registers in circuit "%s", ' 'counts will be empty.', name) elif 'measure' not in [op.name for op in experiment.instructions]: logger.warning('No measurements in circuit "%s", ' 'classical register will remain all zeros.', name)
python
{ "resource": "" }
q268353
UnitarySimulatorPy._validate_initial_unitary
test
def _validate_initial_unitary(self): """Validate an initial unitary matrix""" # If initial unitary isn't set we don't need to validate if self._initial_unitary is None: return # Check unitary is correct length for number of qubits shape = np.shape(self._initial_unitary) required_shape = (2 ** self._number_of_qubits, 2 ** self._number_of_qubits) if shape != required_shape: raise BasicAerError('initial unitary is incorrect shape: ' + '{} != 2 ** {}'.format(shape, required_shape))
python
{ "resource": "" }
q268354
UnitarySimulatorPy._initialize_unitary
test
def _initialize_unitary(self): """Set the initial unitary for simulation""" self._validate_initial_unitary() if self._initial_unitary is None: # Set to identity matrix self._unitary = np.eye(2 ** self._number_of_qubits, dtype=complex) else: self._unitary = self._initial_unitary.copy() # Reshape to rank-N tensor self._unitary = np.reshape(self._unitary, self._number_of_qubits * [2, 2])
python
{ "resource": "" }
q268355
UnitarySimulatorPy._get_unitary
test
def _get_unitary(self): """Return the current unitary in JSON Result spec format""" unitary = np.reshape(self._unitary, 2 * [2 ** self._number_of_qubits]) # Expand complex numbers unitary = np.stack((unitary.real, unitary.imag), axis=-1) # Truncate small values unitary[abs(unitary) < self._chop_threshold] = 0.0 return unitary
python
{ "resource": "" }
q268356
UnitarySimulatorPy._run_job
test
def _run_job(self, job_id, qobj): """Run experiments in qobj. Args: job_id (str): unique id for the job. qobj (Qobj): job description Returns: Result: Result object """ self._validate(qobj) result_list = [] start = time.time() for experiment in qobj.experiments: result_list.append(self.run_experiment(experiment)) end = time.time() result = {'backend_name': self.name(), 'backend_version': self._configuration.backend_version, 'qobj_id': qobj.qobj_id, 'job_id': job_id, 'results': result_list, 'status': 'COMPLETED', 'success': True, 'time_taken': (end - start), 'header': qobj.header.as_dict()} return Result.from_dict(result)
python
{ "resource": "" }
q268357
UnitarySimulatorPy._validate
test
def _validate(self, qobj): """Semantic validations of the qobj which cannot be done via schemas. Some of these may later move to backend schemas. 1. No shots 2. No measurements in the middle """ n_qubits = qobj.config.n_qubits max_qubits = self.configuration().n_qubits if n_qubits > max_qubits: raise BasicAerError('Number of qubits {} '.format(n_qubits) + 'is greater than maximum ({}) '.format(max_qubits) + 'for "{}".'.format(self.name())) if hasattr(qobj.config, 'shots') and qobj.config.shots != 1: logger.info('"%s" only supports 1 shot. Setting shots=1.', self.name()) qobj.config.shots = 1 for experiment in qobj.experiments: name = experiment.header.name if getattr(experiment.config, 'shots', 1) != 1: logger.info('"%s" only supports 1 shot. ' 'Setting shots=1 for circuit "%s".', self.name(), name) experiment.config.shots = 1 for operation in experiment.instructions: if operation.name in ['measure', 'reset']: raise BasicAerError('Unsupported "%s" instruction "%s" ' + 'in circuit "%s" ', self.name(), operation.name, name)
python
{ "resource": "" }
q268358
_is_bit
test
def _is_bit(obj): """Determine if obj is a bit""" # If there is a bit type this could be replaced by isinstance. if isinstance(obj, tuple) and len(obj) == 2: if isinstance(obj[0], Register) and isinstance(obj[1], int) and obj[1] < len(obj[0]): return True return False
python
{ "resource": "" }
q268359
TrivialLayout.run
test
def run(self, dag): """ Pick a layout by assigning n circuit qubits to device qubits 0, .., n-1. Args: dag (DAGCircuit): DAG to find layout for. Raises: TranspilerError: if dag wider than self.coupling_map """ num_dag_qubits = sum([qreg.size for qreg in dag.qregs.values()]) if num_dag_qubits > self.coupling_map.size(): raise TranspilerError('Number of qubits greater than device.') self.property_set['layout'] = Layout.generate_trivial_layout(*dag.qregs.values())
python
{ "resource": "" }
q268360
Interval.has_overlap
test
def has_overlap(self, interval: 'Interval') -> bool: """Check if self has overlap with `interval`. Args: interval: interval to be examined Returns: bool: True if self has overlap with `interval` otherwise False """ if self.begin < interval.end and interval.begin < self.end: return True return False
python
{ "resource": "" }
q268361
Interval.shift
test
def shift(self, time: int) -> 'Interval': """Return a new interval shifted by `time` from self Args: time: time to be shifted Returns: Interval: interval shifted by `time` """ return Interval(self._begin + time, self._end + time)
python
{ "resource": "" }
q268362
Timeslot.shift
test
def shift(self, time: int) -> 'Timeslot': """Return a new Timeslot shifted by `time`. Args: time: time to be shifted """ return Timeslot(self.interval.shift(time), self.channel)
python
{ "resource": "" }
q268363
TimeslotCollection.ch_start_time
test
def ch_start_time(self, *channels: List[Channel]) -> int: """Return earliest start time in this collection. Args: *channels: Channels over which to obtain start_time. """ intervals = list(itertools.chain(*(self._table[chan] for chan in channels if chan in self._table))) if intervals: return min((interval.begin for interval in intervals)) return 0
python
{ "resource": "" }
q268364
TimeslotCollection.ch_stop_time
test
def ch_stop_time(self, *channels: List[Channel]) -> int: """Return maximum time of timeslots over all channels. Args: *channels: Channels over which to obtain stop time. """ intervals = list(itertools.chain(*(self._table[chan] for chan in channels if chan in self._table))) if intervals: return max((interval.end for interval in intervals)) return 0
python
{ "resource": "" }
q268365
TimeslotCollection.is_mergeable_with
test
def is_mergeable_with(self, timeslots: 'TimeslotCollection') -> bool: """Return if self is mergeable with `timeslots`. Args: timeslots: TimeslotCollection to be checked """ for slot in timeslots.timeslots: for interval in self._table[slot.channel]: if slot.interval.has_overlap(interval): return False return True
python
{ "resource": "" }
q268366
TimeslotCollection.merged
test
def merged(self, timeslots: 'TimeslotCollection') -> 'TimeslotCollection': """Return a new TimeslotCollection merged with a specified `timeslots` Args: timeslots: TimeslotCollection to be merged """ slots = [Timeslot(slot.interval, slot.channel) for slot in self.timeslots] slots.extend([Timeslot(slot.interval, slot.channel) for slot in timeslots.timeslots]) return TimeslotCollection(*slots)
python
{ "resource": "" }
q268367
TimeslotCollection.shift
test
def shift(self, time: int) -> 'TimeslotCollection': """Return a new TimeslotCollection shifted by `time`. Args: time: time to be shifted by """ slots = [Timeslot(slot.interval.shift(time), slot.channel) for slot in self.timeslots] return TimeslotCollection(*slots)
python
{ "resource": "" }
q268368
CIFailureReporter.report
test
def report(self, branch, commit, infourl=None): """Report on GitHub that the specified branch is failing to build at the specified commit. The method will open an issue indicating that the branch is failing. If there is an issue already open, it will add a comment avoiding to report twice about the same failure. Args: branch (str): branch name to report about. commit (str): commit hash at which the build fails. infourl (str): URL with extra info about the failure such as the build logs. """ issue_number = self._get_report_issue_number() if issue_number: self._report_as_comment(issue_number, branch, commit, infourl) else: self._report_as_issue(branch, commit, infourl)
python
{ "resource": "" }
q268369
process_data
test
def process_data(rho): """ Sort rho data """ result = dict() num = int(np.log2(len(rho))) labels = list(map(lambda x: x.to_label(), pauli_group(num))) values = list(map(lambda x: np.real(np.trace(np.dot(x.to_matrix(), rho))), pauli_group(num))) for position, label in enumerate(labels): result[label] = values[position] return result
python
{ "resource": "" }
q268370
iplot_state_paulivec
test
def iplot_state_paulivec(rho, figsize=None, slider=False, show_legend=False): """ Create a paulivec representation. Graphical representation of the input array. Args: rho (array): State vector or density matrix. figsize (tuple): Figure size in pixels. slider (bool): activate slider show_legend (bool): show legend of graph content """ # HTML html_template = Template(""" <p> <div id="paulivec_$divNumber"></div> </p> """) # JavaScript javascript_template = Template(""" <script> requirejs.config({ paths: { qVisualization: "https://qvisualization.mybluemix.net/q-visualizations" } }); require(["qVisualization"], function(qVisualizations) { qVisualizations.plotState("paulivec_$divNumber", "paulivec", $executions, $options); }); </script> """) rho = _validate_input_state(rho) # set default figure size if none given if figsize is None: figsize = (7, 5) options = {'width': figsize[0], 'height': figsize[1], 'slider': int(slider), 'show_legend': int(show_legend)} # Process data and execute div_number = str(time.time()) div_number = re.sub('[.]', '', div_number) data_to_plot = [] rho_data = process_data(rho) data_to_plot.append(dict( data=rho_data )) html = html_template.substitute({ 'divNumber': div_number }) javascript = javascript_template.substitute({ 'divNumber': div_number, 'executions': data_to_plot, 'options': options }) display(HTML(html + javascript))
python
{ "resource": "" }
q268371
rzz
test
def rzz(self, theta, qubit1, qubit2): """Apply RZZ to circuit.""" return self.append(RZZGate(theta), [qubit1, qubit2], [])
python
{ "resource": "" }
q268372
cswap
test
def cswap(self, ctl, tgt1, tgt2): """Apply Fredkin to circuit.""" return self.append(FredkinGate(), [ctl, tgt1, tgt2], [])
python
{ "resource": "" }
q268373
NoiseAdaptiveLayout._initialize_backend_prop
test
def _initialize_backend_prop(self): """ Extract readout and CNOT errors and compute swap costs. """ backend_prop = self.backend_prop for ginfo in backend_prop.gates: if ginfo.gate == 'cx': for item in ginfo.parameters: if item.name == 'gate_error': g_reliab = 1.0 - item.value break else: g_reliab = 1.0 swap_reliab = -math.log(pow(g_reliab, 3)) self.swap_graph.add_edge(ginfo.qubits[0], ginfo.qubits[1], weight=swap_reliab) self.swap_graph.add_edge(ginfo.qubits[1], ginfo.qubits[0], weight=swap_reliab) self.cx_errors[(ginfo.qubits[0], ginfo.qubits[1])] = g_reliab self.gate_list.append((ginfo.qubits[0], ginfo.qubits[1])) idx = 0 for q in backend_prop.qubits: for nduv in q: if nduv.name == 'readout_error': self.readout_errors[idx] = 1.0 - nduv.value self.available_hw_qubits.append(idx) idx += 1 for edge in self.cx_errors: self.gate_cost[edge] = self.cx_errors[edge] * self.readout_errors[edge[0]] *\ self.readout_errors[edge[1]] self.swap_paths, swap_costs_temp = nx.algorithms.shortest_paths.dense.\ floyd_warshall_predecessor_and_distance(self.swap_graph, weight='weight') for i in swap_costs_temp: self.swap_costs[i] = {} for j in swap_costs_temp[i]: if (i, j) in self.cx_errors: self.swap_costs[i][j] = self.cx_errors[(i, j)] elif (j, i) in self.cx_errors: self.swap_costs[i][j] = self.cx_errors[(j, i)] else: best_reliab = 0.0 for n in self.swap_graph.neighbors(j): if (n, j) in self.cx_errors: reliab = math.exp(-swap_costs_temp[i][n])*self.cx_errors[(n, j)] else: reliab = math.exp(-swap_costs_temp[i][n])*self.cx_errors[(j, n)] if reliab > best_reliab: best_reliab = reliab self.swap_costs[i][j] = best_reliab
python
{ "resource": "" }
q268374
NoiseAdaptiveLayout._create_program_graph
test
def _create_program_graph(self, dag): """ Program graph has virtual qubits as nodes. Two nodes have an edge if the corresponding virtual qubits participate in a 2-qubit gate. The edge is weighted by the number of CNOTs between the pair. """ idx = 0 for q in dag.qubits(): self.qarg_to_id[q[0].name + str(q[1])] = idx idx += 1 for gate in dag.twoQ_gates(): qid1 = self._qarg_to_id(gate.qargs[0]) qid2 = self._qarg_to_id(gate.qargs[1]) min_q = min(qid1, qid2) max_q = max(qid1, qid2) edge_weight = 1 if self.prog_graph.has_edge(min_q, max_q): edge_weight = self.prog_graph[min_q][max_q]['weight'] + 1 self.prog_graph.add_edge(min_q, max_q, weight=edge_weight) return idx
python
{ "resource": "" }
q268375
NoiseAdaptiveLayout._select_next_edge
test
def _select_next_edge(self): """ If there is an edge with one endpoint mapped, return it. Else return in the first edge """ for edge in self.pending_program_edges: q1_mapped = edge[0] in self.prog2hw q2_mapped = edge[1] in self.prog2hw assert not (q1_mapped and q2_mapped) if q1_mapped or q2_mapped: return edge return self.pending_program_edges[0]
python
{ "resource": "" }
q268376
NoiseAdaptiveLayout._select_best_remaining_cx
test
def _select_best_remaining_cx(self): """ Select best remaining CNOT in the hardware for the next program edge. """ candidates = [] for gate in self.gate_list: chk1 = gate[0] in self.available_hw_qubits chk2 = gate[1] in self.available_hw_qubits if chk1 and chk2: candidates.append(gate) best_reliab = 0 best_item = None for item in candidates: if self.gate_cost[item] > best_reliab: best_reliab = self.gate_cost[item] best_item = item return best_item
python
{ "resource": "" }
q268377
NoiseAdaptiveLayout._select_best_remaining_qubit
test
def _select_best_remaining_qubit(self, prog_qubit): """ Select the best remaining hardware qubit for the next program qubit. """ reliab_store = {} for hw_qubit in self.available_hw_qubits: reliab = 1 for n in self.prog_graph.neighbors(prog_qubit): if n in self.prog2hw: reliab *= self.swap_costs[self.prog2hw[n]][hw_qubit] reliab *= self.readout_errors[hw_qubit] reliab_store[hw_qubit] = reliab max_reliab = 0 best_hw_qubit = None for hw_qubit in reliab_store: if reliab_store[hw_qubit] > max_reliab: max_reliab = reliab_store[hw_qubit] best_hw_qubit = hw_qubit return best_hw_qubit
python
{ "resource": "" }
q268378
NoiseAdaptiveLayout.run
test
def run(self, dag): """Main run method for the noise adaptive layout.""" self._initialize_backend_prop() num_qubits = self._create_program_graph(dag) if num_qubits > len(self.swap_graph): raise TranspilerError('Number of qubits greater than device.') for end1, end2, _ in sorted(self.prog_graph.edges(data=True), key=lambda x: x[2]['weight'], reverse=True): self.pending_program_edges.append((end1, end2)) while self.pending_program_edges: edge = self._select_next_edge() q1_mapped = edge[0] in self.prog2hw q2_mapped = edge[1] in self.prog2hw if (not q1_mapped) and (not q2_mapped): best_hw_edge = self._select_best_remaining_cx() self.prog2hw[edge[0]] = best_hw_edge[0] self.prog2hw[edge[1]] = best_hw_edge[1] self.available_hw_qubits.remove(best_hw_edge[0]) self.available_hw_qubits.remove(best_hw_edge[1]) elif not q1_mapped: best_hw_qubit = self._select_best_remaining_qubit(edge[0]) self.prog2hw[edge[0]] = best_hw_qubit self.available_hw_qubits.remove(best_hw_qubit) else: best_hw_qubit = self._select_best_remaining_qubit(edge[1]) self.prog2hw[edge[1]] = best_hw_qubit self.available_hw_qubits.remove(best_hw_qubit) new_edges = [x for x in self.pending_program_edges if not (x[0] in self.prog2hw and x[1] in self.prog2hw)] self.pending_program_edges = new_edges for qid in self.qarg_to_id.values(): if qid not in self.prog2hw: self.prog2hw[qid] = self.available_hw_qubits[0] self.available_hw_qubits.remove(self.prog2hw[qid]) layout = Layout() for q in dag.qubits(): pid = self._qarg_to_id(q) hwid = self.prog2hw[pid] layout[(q[0], q[1])] = hwid self.property_set['layout'] = layout
python
{ "resource": "" }
q268379
CompositeGate.instruction_list
test
def instruction_list(self): """Return a list of instructions for this CompositeGate. If the CompositeGate itself contains composites, call this method recursively. """ instruction_list = [] for instruction in self.data: if isinstance(instruction, CompositeGate): instruction_list.extend(instruction.instruction_list()) else: instruction_list.append(instruction) return instruction_list
python
{ "resource": "" }
q268380
CompositeGate.inverse
test
def inverse(self): """Invert this gate.""" self.data = [gate.inverse() for gate in reversed(self.data)] self.inverse_flag = not self.inverse_flag return self
python
{ "resource": "" }
q268381
CompositeGate.q_if
test
def q_if(self, *qregs): """Add controls to this gate.""" self.data = [gate.q_if(qregs) for gate in self.data] return self
python
{ "resource": "" }
q268382
CompositeGate.c_if
test
def c_if(self, classical, val): """Add classical control register.""" self.data = [gate.c_if(classical, val) for gate in self.data] return self
python
{ "resource": "" }
q268383
Operator.is_unitary
test
def is_unitary(self, atol=None, rtol=None): """Return True if operator is a unitary matrix.""" if atol is None: atol = self._atol if rtol is None: rtol = self._rtol return is_unitary_matrix(self._data, rtol=rtol, atol=atol)
python
{ "resource": "" }
q268384
Operator.conjugate
test
def conjugate(self): """Return the conjugate of the operator.""" return Operator( np.conj(self.data), self.input_dims(), self.output_dims())
python
{ "resource": "" }
q268385
Operator.transpose
test
def transpose(self): """Return the transpose of the operator.""" return Operator( np.transpose(self.data), self.input_dims(), self.output_dims())
python
{ "resource": "" }
q268386
Operator.power
test
def power(self, n): """Return the matrix power of the operator. Args: n (int): the power to raise the matrix to. Returns: BaseOperator: the n-times composed operator. Raises: QiskitError: if the input and output dimensions of the operator are not equal, or the power is not a positive integer. """ if not isinstance(n, int): raise QiskitError("Can only take integer powers of Operator.") if self.input_dims() != self.output_dims(): raise QiskitError("Can only power with input_dims = output_dims.") # Override base class power so we can implement more efficiently # using Numpy.matrix_power return Operator( np.linalg.matrix_power(self.data, n), self.input_dims(), self.output_dims())
python
{ "resource": "" }
q268387
Operator._shape
test
def _shape(self): """Return the tensor shape of the matrix operator""" return tuple(reversed(self.output_dims())) + tuple( reversed(self.input_dims()))
python
{ "resource": "" }
q268388
Operator._instruction_to_operator
test
def _instruction_to_operator(cls, instruction): """Convert a QuantumCircuit or Instruction to an Operator.""" # Convert circuit to an instruction if isinstance(instruction, QuantumCircuit): instruction = instruction.to_instruction() # Initialize an identity operator of the correct size of the circuit op = Operator(np.eye(2 ** instruction.num_qubits)) op._append_instruction(instruction) return op
python
{ "resource": "" }
q268389
LegacySwap.swap_mapper_layer_update
test
def swap_mapper_layer_update(self, i, first_layer, best_layout, best_d, best_circ, layer_list): """Update the QASM string for an iteration of swap_mapper. i = layer number first_layer = True if this is the first layer with multi-qubit gates best_layout = layout returned from swap algorithm best_d = depth returned from swap algorithm best_circ = swap circuit returned from swap algorithm layer_list = list of circuit objects for each layer Return DAGCircuit object to append to the output DAGCircuit. """ layout = best_layout dagcircuit_output = DAGCircuit() QR = QuantumRegister(self.coupling_map.size(), 'q') dagcircuit_output.add_qreg(QR) # Identity wire-map for composing the circuits identity_wire_map = {(QR, j): (QR, j) for j in range(self.coupling_map.size())} # If this is the first layer with multi-qubit gates, # output all layers up to this point and ignore any # swap gates. Set the initial layout. if first_layer: # Output all layers up to this point for j in range(i + 1): dagcircuit_output.compose_back(layer_list[j]["graph"], layout) # Otherwise, we output the current layer and the associated swap gates. else: # Output any swaps if best_d > 0: dagcircuit_output.compose_back(best_circ, identity_wire_map) # Output this layer dagcircuit_output.compose_back(layer_list[i]["graph"], layout) return dagcircuit_output
python
{ "resource": "" }
q268390
_separate_bitstring
test
def _separate_bitstring(bitstring, creg_sizes): """Separate a bitstring according to the registers defined in the result header.""" substrings = [] running_index = 0 for _, size in reversed(creg_sizes): substrings.append(bitstring[running_index: running_index + size]) running_index += size return ' '.join(substrings)
python
{ "resource": "" }
q268391
format_level_0_memory
test
def format_level_0_memory(memory): """ Format an experiment result memory object for measurement level 0. Args: memory (list): Memory from experiment with `meas_level==1`. `avg` or `single` will be inferred from shape of result memory. Returns: np.ndarray: Measurement level 0 complex numpy array Raises: QiskitError: If the returned numpy array does not have 2 (avg) or 3 (single) indicies. """ formatted_memory = _list_to_complex_array(memory) # infer meas_return from shape of returned data. if not 2 <= len(formatted_memory.shape) <= 3: raise QiskitError('Level zero memory is not of correct shape.') return formatted_memory
python
{ "resource": "" }
q268392
format_level_1_memory
test
def format_level_1_memory(memory): """ Format an experiment result memory object for measurement level 1. Args: memory (list): Memory from experiment with `meas_level==1`. `avg` or `single` will be inferred from shape of result memory. Returns: np.ndarray: Measurement level 1 complex numpy array Raises: QiskitError: If the returned numpy array does not have 1 (avg) or 2 (single) indicies. """ formatted_memory = _list_to_complex_array(memory) # infer meas_return from shape of returned data. if not 1 <= len(formatted_memory.shape) <= 2: raise QiskitError('Level one memory is not of correct shape.') return formatted_memory
python
{ "resource": "" }
q268393
format_level_2_memory
test
def format_level_2_memory(memory, header=None): """ Format an experiment result memory object for measurement level 2. Args: memory (list): Memory from experiment with `meas_level==2` and `memory==True`. header (dict): the experiment header dictionary containing useful information for postprocessing. Returns: list[str]: List of bitstrings """ memory_list = [] for shot_memory in memory: memory_list.append(format_counts_memory(shot_memory, header)) return memory_list
python
{ "resource": "" }
q268394
format_counts
test
def format_counts(counts, header=None): """Format a single experiment result coming from backend to present to the Qiskit user. Args: counts (dict): counts histogram of multiple shots header (dict): the experiment header dictionary containing useful information for postprocessing. Returns: dict: a formatted counts """ counts_dict = {} for key, val in counts.items(): key = format_counts_memory(key, header) counts_dict[key] = val return counts_dict
python
{ "resource": "" }
q268395
format_statevector
test
def format_statevector(vec, decimals=None): """Format statevector coming from the backend to present to the Qiskit user. Args: vec (list): a list of [re, im] complex numbers. decimals (int): the number of decimals in the statevector. If None, no rounding is done. Returns: list[complex]: a list of python complex numbers. """ num_basis = len(vec) vec_complex = np.zeros(num_basis, dtype=complex) for i in range(num_basis): vec_complex[i] = vec[i][0] + 1j * vec[i][1] if decimals: vec_complex = np.around(vec_complex, decimals=decimals) return vec_complex
python
{ "resource": "" }
q268396
format_unitary
test
def format_unitary(mat, decimals=None): """Format unitary coming from the backend to present to the Qiskit user. Args: mat (list[list]): a list of list of [re, im] complex numbers decimals (int): the number of decimals in the statevector. If None, no rounding is done. Returns: list[list[complex]]: a matrix of complex numbers """ num_basis = len(mat) mat_complex = np.zeros((num_basis, num_basis), dtype=complex) for i, vec in enumerate(mat): mat_complex[i] = format_statevector(vec, decimals) return mat_complex
python
{ "resource": "" }
q268397
requires_submit
test
def requires_submit(func): """ Decorator to ensure that a submit has been performed before calling the method. Args: func (callable): test function to be decorated. Returns: callable: the decorated function. """ @functools.wraps(func) def _wrapper(self, *args, **kwargs): if self._future is None: raise JobError("Job not submitted yet!. You have to .submit() first!") return func(self, *args, **kwargs) return _wrapper
python
{ "resource": "" }
q268398
BasicAerJob.submit
test
def submit(self): """Submit the job to the backend for execution. Raises: QobjValidationError: if the JSON serialization of the Qobj passed during construction does not validate against the Qobj schema. JobError: if trying to re-submit the job. """ if self._future is not None: raise JobError("We have already submitted the job!") validate_qobj_against_schema(self._qobj) self._future = self._executor.submit(self._fn, self._job_id, self._qobj)
python
{ "resource": "" }
q268399
BasicAerJob.status
test
def status(self): """Gets the status of the job by querying the Python's future Returns: qiskit.providers.JobStatus: The current JobStatus Raises: JobError: If the future is in unexpected state concurrent.futures.TimeoutError: if timeout occurred. """ # The order is important here if self._future.running(): _status = JobStatus.RUNNING elif self._future.cancelled(): _status = JobStatus.CANCELLED elif self._future.done(): _status = JobStatus.DONE if self._future.exception() is None else JobStatus.ERROR else: # Note: There is an undocumented Future state: PENDING, that seems to show up when # the job is enqueued, waiting for someone to pick it up. We need to deal with this # state but there's no public API for it, so we are assuming that if the job is not # in any of the previous states, is PENDING, ergo INITIALIZING for us. _status = JobStatus.INITIALIZING return _status
python
{ "resource": "" }