def pyquest_call_operation( operation: Any, qureg: tqureg, classical_bit_registers: Dict[str, List[bool]], classical_float_registers: Dict[str, List[float]], classical_complex_registers: Dict[str, List[complex]], output_bit_register_dict: Dict[str, List[List[bool]]], ) -> None: """Execute the qoqo operation with PyQuEST Args: operation: The qoqo operation that is executed qureg: The quantum register pyquest_cffi operates on classical_bit_registers: Dictionary or registers (lists) containing bit readout values classical_float_registers: Dictionary or registers (lists) containing float readout values classical_complex_registers: Dictionary or registers (lists) containing complex readout values output_bit_register_dict: Dictionary or lists of registers (lists) containing a register for each repetition of the circuit Raises: RuntimeError: Operation not in PyQuEST backend """ tags = operation.tags() if 'RotateZ' in tags: operation = cast(ops.RotateZ, operation) qops.rotateZ()(qureg=qureg, qubit=operation.qubit(), theta=operation.theta().float()) elif 'RotateX' in tags: operation = cast(ops.RotateX, operation) qops.rotateX()(qureg=qureg, qubit=operation.qubit(), theta=operation.theta().float()) elif 'RotateY' in tags: operation = cast(ops.RotateY, operation) qops.rotateY()(qureg=qureg, qubit=operation.qubit(), theta=operation.theta().float()) elif 'SqrtISwap' in tags: operation = cast(ops.SqrtISwap, operation) qops.sqrtISwap()(qureg=qureg, control=operation.control(), qubit=operation.target()) elif 'CNOT' in tags: operation = cast(ops.CNOT, operation) qops.controlledNot()(qureg=qureg, control=operation.control(), qubit=operation.target()) elif 'InvSqrtISwap' in tags: operation = cast(ops.InvSqrtISwap, operation) qops.invSqrtISwap()(qureg=qureg, control=operation.control(), qubit=operation.target()) elif 'Hadamard' in tags: operation = cast(ops.Hadamard, operation) qops.hadamard()(qureg=qureg, qubit=operation.qubit()) elif 'PauliX' in tags: operation = cast(ops.PauliX, operation) qops.pauliX()(qureg=qureg, qubit=operation.qubit()) elif 'PauliY' in tags: operation = cast(ops.PauliY, operation) qops.pauliY()(qureg=qureg, qubit=operation.qubit()) elif 'PauliZ' in tags: operation = cast(ops.PauliZ, operation) qops.pauliZ()(qureg=qureg, qubit=operation.qubit()) elif 'SGate' in tags: operation = cast(ops.SGate, operation) qops.sGate()(qureg=qureg, qubit=operation.qubit()) elif 'TGate' in tags: operation = cast(ops.TGate, operation) qops.tGate()(qureg=qureg, qubit=operation.qubit()) elif 'SqrtPauliX' in tags: qops.rotateX()(qureg=qureg, qubit=operation.qubit(), theta=np.pi / 2) elif 'InvSqrtPauliX' in tags: qops.rotateX()(qureg=qureg, qubit=operation.qubit(), theta=-np.pi / 2) elif 'ControlledPhaseShift' in tags: qops.controlledPhaseShift()(qureg=qureg, control=operation.control(), qubit=operation.target(), theta=operation.theta().float()) elif 'ControlledPauliY' in tags: qops.controlledPauliY()(qureg=qureg, control=operation.control(), qubit=operation.target()) elif 'ControlledPauliZ' in tags: qops.controlledPhaseFlip()(qureg=qureg, control=operation.control(), qubit=operation.target()) elif 'RotateAroundSphericalAxis' in tags: qops.rotateAroundSphericalAxis()( qureg=qureg, qubit=operation.qubit(), theta=operation.theta().float(), spherical_theta=operation.spherical_theta().float(), spherical_phi=operation.spherical_phi().float()) elif 'SingleQubitGateOperation' in tags: qops.compactUnitary()(qubit=operation.qubit(), qureg=qureg, alpha=operation.alpha_r().float() + 1j * operation.alpha_i().float(), beta=operation.beta_r().float() + 1j * operation.beta_i().float()) elif 'TwoQubitGateOperation' in tags: qops.twoQubitUnitary()(qureg=qureg, target_qubit_2=operation.control(), target_qubit_1=operation.target(), matrix=operation.unitary_matrix()) elif 'PragmaRepeatedMeasurement' in tags: _execute_PragmaRepeatedMeasurement( operation, qureg, classical_bit_registers, output_bit_register_dict, ) elif 'PragmaDamping' in tags: qops.mixDamping()( qureg=qureg, qubit=operation.qubit(), probability=operation.probability().float()) elif 'PragmaDepolarising' in tags: qops.mixDepolarising()( qureg=qureg, qubit=operation.qubit(), probability=operation.probability().float()) elif 'PragmaDephasing' in tags: qops.mixDephasing()( qureg=qureg, qubit=operation.qubit(), probability=operation.probability().float()) elif 'PragmaSetStateVector' in tags: vector = operation.statevector() qcheat.initStateFromAmps()(qureg=qureg, reals=np.real(vector), imags=np.imag(vector)) elif 'PragmaSetDensityMatrix' in tags: density_matrix = operation.density_matrix() # dim = int(np.round(np.sqrt(density_matrix.shape[0]))) # density_matrix = density_matrix.reshape((dim, dim)) qcheat.initStateFromAmps()( qureg=qureg, reals=np.real(density_matrix), imags=np.imag(density_matrix)) elif 'PragmaRandomNoise' in tags: _execute_PragmaRandomNoise( operation, qureg, ) elif 'PragmaActiveReset' in tags: _execute_PragmaActiveReset( operation, qureg, ) elif 'MeasureQubit' in tags: _execute_MeasureQubit( operation, qureg, classical_bit_registers, ) elif 'InputDefinition' in tags: if operation.name() in classical_float_registers.keys(): classical_float_registers = operation.input() elif 'Definition' in tags: pass elif 'PragmaGetPauliProduct' in tags: _execute_GetPauliProduct( operation, qureg, classical_float_registers, ) elif 'PragmaGetStateVector' in tags: _execute_GetStateVector( operation, qureg, classical_complex_registers, ) elif 'PragmaGetDensityMatrix' in tags: _execute_GetStateVector( operation, qureg, classical_complex_registers, ) elif 'PragmaGetOccupationProbability' in tags: _execute_GetOccupationProbability( operation, qureg, classical_float_registers, ) elif 'PragmaGetRotatedOccupationProbability' in tags: _execute_GetOccupationProbability( operation, qureg, classical_float_registers, ) elif 'PragmaConditional' in tags: cast(ops.PragmaConditional, operation) if operation.condition_register() not in classical_bit_registers.keys(): raise RuntimeError( "Conditional register {} not found in classical bit registers".format( operation.condition_register())) if classical_bit_registers[operation.condition_register()][operation.condition_index()]: pyquest_call_circuit( circuit=operation.circuit(), qureg=qureg, classical_bit_registers=classical_bit_registers, classical_float_registers=classical_float_registers, classical_complex_registers=classical_complex_registers, output_bit_register_dict=output_bit_register_dict, ) elif any(pragma in tags for pragma in _ALLOWED_PRAGMAS): pass else: raise RuntimeError('Operation not in PyQuEST backend')
def run_example_interactive(): """Example function Running the exact same Example QuEST provides in the QuEST git repository with the interactive python interface of PyQuEST-cffi """ print('PyQuEST-cffi tutorial based on QuEST tutorial') print(' Basic 3 qubit circuit') # creating environment env = utils.createQuestEnv()() # allocating qubit register qureg = utils.createQureg()(3, env=env) cheat.initZeroState()(qureg=qureg) # Using the report function to print system status print('This is the environment:') reporting.reportQuESTEnv()(env=env) print('This is the qubit register:') reporting.reportQuregParams()(qureg=qureg) print('This we can easily do in interactive python:') print('Result of qureg.isDensityMatrix: ', qureg.isDensityMatrix) # Apply circuit ops.hadamard()(qureg=qureg, qubit=0) ops.controlledNot()(qureg=qureg, control=0, qubit=1) ops.rotateY()(qureg=qureg, qubit=2, theta=0.1) ops.multiControlledPhaseFlip()(qureg=qureg, controls=[0, 1, 2], number_controls=3) u = np.zeros((2, 2), dtype=complex) u[0, 0] = 0.5 * (1 + 1j) u[0, 1] = 0.5 * (1 - 1j) u[1, 0] = 0.5 * (1 - 1j) u[1, 1] = 0.5 * (1 + 1j) ops.unitary()(qureg=qureg, qubit=0, matrix=u) a = 0.5 + 0.5 * 1j b = 0.5 - 0.5 * 1j ops.compactUnitary()(qureg=qureg, qubit=1, alpha=a, beta=b) v = np.array([1, 0, 0]) ops.rotateAroundAxis()(qureg=qureg, qubit=2, theta=np.pi / 2, vector=v) ops.controlledCompactUnitary()(qureg=qureg, control=0, qubit=1, alpha=a, beta=b) ops.multiControlledUnitary()(qureg=qureg, controls=[0, 1], number_controls=2, qubit=2, matrix=u) # cheated results print('Circuit output') print('Probability amplitude of |111> by knowing the index: ', cheat.getProbAmp()(qureg=qureg, index=7)) print('Probability amplitude of |111> by referencing basis state: ', cheat.getProbAmp()(qureg=qureg, index=[1, 1, 1])) # measuring: measurement = ops.measure()(qureg=qureg, qubit=0) print('Qubit 0 was measured as: ', measurement)