def test_parse_reset_qubit(): reset = """ RESET """.strip() parse_equals(reset, Reset()) reset_qubit = """ RESET 5 """.strip() parse_equals(reset_qubit, ResetQubit(Qubit(5)))
def RESET(qubit_index=None): """ Reset all qubits or just a specific qubit at qubit_index. :param Optional[int] qubit_index: The address of the qubit to reset. If None, reset all qubits. :returns: A Reset or ResetQubit Quil AST expression corresponding to a global or targeted reset, respectively. :rtype: Union[Reset, ResetQubit] """ if qubit_index is not None: return ResetQubit(Qubit(qubit_index)) else: return Reset()
def RESET(qubit_index: Optional[QubitDesignator] = None) -> Union[Reset, ResetQubit]: """ Reset all qubits or just one specific qubit. :param qubit_index: The qubit to reset. This can be a qubit's index, a Qubit, or a QubitPlaceholder. If None, reset all qubits. :returns: A Reset or ResetQubit Quil AST expression corresponding to a global or targeted reset, respectively. """ if qubit_index is not None: return ResetQubit(unpack_qubit(qubit_index)) else: return Reset()
def RESET(qubit_index=None): """ Reset all qubits or just one specific qubit. :param Optional[Union[integer_types, Qubit, QubitPlaceholder]] qubit_index: The qubit to reset. This can be a qubit's index, a Qubit, or a QubitPlaceholder. If None, reset all qubits. :returns: A Reset or ResetQubit Quil AST expression corresponding to a global or targeted reset, respectively. :rtype: Union[Reset, ResetQubit] """ if qubit_index is not None: return ResetQubit(unpack_qubit(qubit_index)) else: return Reset()
def reset(self, qubit): if qubit: return ResetQubit(qubit) else: return Reset()
def exitResetState(self, ctx): # type: (QuilParser.ResetStateContext) -> None if ctx.qubit(): self.result.append(ResetQubit(_qubit(ctx.qubit()))) else: self.result.append(Reset())
def address_qubits(program, qubit_mapping=None): """ Takes a program which contains placeholders and assigns them all defined values. Either all qubits must be defined or all undefined. If qubits are undefined, you may provide a qubit mapping to specify how placeholders get mapped to actual qubits. If a mapping is not provided, integers 0 through N are used. This function will also instantiate any label placeholders. :param program: The program. :param qubit_mapping: A dictionary-like object that maps from :py:class:`QubitPlaceholder` to :py:class:`Qubit` or ``int`` (but not both). :return: A new Program with all qubit and label placeholders assigned to real qubits and labels. """ fake_qubits, real_qubits, qubits = _what_type_of_qubit_does_it_use(program) if real_qubits: if qubit_mapping is not None: warnings.warn( "A qubit mapping was provided but the program does not " "contain any placeholders to map!") return program if qubit_mapping is None: qubit_mapping = {qp: Qubit(i) for i, qp in enumerate(qubits)} else: if all(isinstance(v, Qubit) for v in qubit_mapping.values()): pass # we good elif all(isinstance(v, int) for v in qubit_mapping.values()): qubit_mapping = {k: Qubit(v) for k, v in qubit_mapping.items()} else: raise ValueError( "Qubit mapping must map to type Qubit or int (but not both)") result = [] for instr in program: # Remap qubits on Gate, Measurement, and ResetQubit instructions if isinstance(instr, Gate): remapped_qubits = [qubit_mapping[q] for q in instr.qubits] gate = Gate(instr.name, instr.params, remapped_qubits) gate.modifiers = instr.modifiers result.append(gate) elif isinstance(instr, Measurement): result.append( Measurement(qubit_mapping[instr.qubit], instr.classical_reg)) elif isinstance(instr, ResetQubit): result.append(ResetQubit(qubit_mapping[instr.qubit])) elif isinstance(instr, Pragma): new_args = [] for arg in instr.args: # Pragmas can have arguments that represent things besides qubits, so here we # make sure to just look up the QubitPlaceholders. if isinstance(arg, QubitPlaceholder): new_args.append(qubit_mapping[arg]) else: new_args.append(arg) result.append( Pragma(instr.command, new_args, instr.freeform_string)) # Otherwise simply add it to the result else: result.append(instr) new_program = program.copy() new_program._instructions = result return new_program