def test_active_reset(): """Test ActiveReset PRAGMA with PyQuEST interface""" definition = ops.DefinitionBit(name='ro', length=2, is_output=True) circuit = Circuit() circuit += definition circuit += ops.PauliX(qubit=0) circuit_with = circuit + ops.PragmaActiveReset(qubit=0) + ops.PragmaActiveReset(qubit=1) circuit_without = circuit + ops.PauliX(qubit=0) circuit_list = [circuit, circuit_with, circuit_without] for circuit in circuit_list: circuit += ops.MeasureQubit(qubit=0, readout='ro', readout_index=0) circuit += ops.MeasureQubit(qubit=1, readout='ro', readout_index=1) test_dict = {'ro': [False, False]} env = utils.createQuestEnv()() qureg = utils.createQureg()(2, env) results_list_0 = [] results_list_1 = [] for circuit in circuit_list: pyquest_call_circuit(circuit=circuit, qureg=qureg, classical_bit_registers=test_dict, classical_float_registers=dict(), classical_complex_registers=dict(), output_bit_register_dict=dict(),) results_list_0.append(test_dict['ro'][0]) results_list_1.append(test_dict['ro'][1]) assert results_list_0 == [True, False, False] assert results_list_1 == [False, False, False]
def test_qasm_xx(): """Testing the MolmerSorensenXX gate with the QASM backend""" circuit = Circuit() circuit += ops.DefinitionBit(name='ro', length=2, is_output=True) circuit += ops.MolmerSorensenXX(control=0, target=1) circuit += ops.MeasureQubit(qubit=0, readout='ro', readout_index=0) circuit += ops.MeasureQubit(qubit=1, readout='ro', readout_index=1) backend = QasmBackend(number_qubits=2) backend.run_circuit(circuit=circuit, overwrite=True) circuit = [ 'creg ro[2];', 'rxx(pi/2) q[0],q[1];', 'measure q[0] -> ro[0];', 'measure q[1] -> ro[1];' ] npt.assert_equal(backend.circuit, circuit)
def test_pyquest_interactive_density_matrix(): """Test with PyQuEST density matrix""" circuit = Circuit() circuit += ops.DefinitionBit(name='out', length=2, is_output=True) circuit += ops.DefinitionBit(name='out2', length=2, is_output=True) circuit += ops.DefinitionBit(name='notout', length=2, is_output=False) circuit += ops.PauliX(qubit=0) circuit += ops.MeasureQubit(qubit=0, readout='out', readout_index=0) circuit += ops.MeasureQubit(qubit=1, readout='out', readout_index=1) pyquest = PyQuestBackend(number_qubits=2) (output_bit_register_dict, output_float_register_dict, output_complex_register_dict) = pyquest.run_circuit(circuit) assert output_bit_register_dict['out'] == [[True, False]] assert len(output_bit_register_dict['out2']) == 1 assert 'notout' not in output_bit_register_dict.keys()
def test_acceptance_with_qiskit(): """Test gate operations with QASM interface""" circuit = Circuit() circuit += ops.RotateX(0, -np.pi) circuit += ops.RotateY(0, -np.pi) circuit += ops.RotateZ(0, -np.pi) circuit += ops.CNOT(0, 1) circuit += ops.Hadamard(0) circuit += ops.PauliX(0) circuit += ops.PauliY(0) circuit += ops.PauliZ(0) circuit += ops.SGate(0) circuit += ops.TGate(0) circuit += ops.SqrtPauliX(0) circuit += ops.MolmerSorensenXX(0, 1) circuit += ops.ControlledPauliY(0, 1) circuit += ops.ControlledPauliZ(0, 1) circuit += ops.SingleQubitGate(0, 1, 0, 1, 0, 1.0) circuit += ops.PragmaRepeatedMeasurement('ro', 1, None) circuit += ops.MeasureQubit(0, 'ro', 0) circuit += ops.DefinitionFloat(name='rof', length=1, is_output=True) circuit += ops.DefinitionBit(name='ro', length=2, is_output=True) circuit += ops.DefinitionComplex(name='roc', length=1, is_output=True) circuit += ops.InputSymbolic('other', 0) circuit += ops.PragmaSetNumberOfMeasurements(20, 'ro') backend = QasmBackend(number_qubits=2) backend.run_circuit(circuit=circuit, overwrite=True) q_circuit = QuantumCircuit.from_qasm_file( "default_qasm_backend_output.qasm")
def test_qasm_backend(): """Testing the QASM functionality with the QASM backend""" circuit = Circuit() circuit += ops.DefinitionBit(name='ro', length=2, is_output=True) circuit += ops.RotateZ(qubit=0, theta=0) circuit += ops.PauliX(qubit=1) circuit += ops.MeasureQubit(qubit=0, readout='ro', readout_index=0) circuit += ops.MeasureQubit(qubit=1, readout='ro', readout_index=1) backend = QasmBackend(number_qubits=2) backend.run_circuit(circuit=circuit, overwrite=True) circuit = [ 'creg ro[2];', 'rz(0.0) q[0];', 'x q[1];', 'measure q[0] -> ro[0];', 'measure q[1] -> ro[1];' ] npt.assert_equal(backend.circuit, circuit)
def test_pyquest_global_phase(): """Test with PyQuEST""" circuit = Circuit() circuit += ops.DefinitionBit(name='out', length=2, is_output=True) circuit += ops.DefinitionFloat(name='global_phase', length=1, is_output=True) circuit += ops.PauliX(qubit=0) circuit += ops.PragmaGlobalPhase(phase=1) circuit += ops.PragmaGlobalPhase(phase='test') circuit += ops.MeasureQubit(qubit=0, readout='out', readout_index=0) circuit += ops.MeasureQubit(qubit=1, readout='out', readout_index=1) circuit += ops.InputSymbolic(name="test", input=-2) circuit = circuit.substitute_parameters({}) pyquest = PyQuestBackend(number_qubits=2) (output_bit_register_dict, output_float_register_dict, output_complex_register_dict) = pyquest.run_circuit(circuit) assert output_bit_register_dict['out'] == [[True, False]] assert len(output_bit_register_dict['out']) == 1 assert output_float_register_dict['global_phase'][0][0] == -1
def test_measurement(): """Test gate operations without free parameters with PyQuEST interface""" op = ops.MeasureQubit(qubit=0, readout='test', readout_index=0) test_dict = {'test': np.array([0, 0, 0, 0])} env = utils.createQuestEnv()() qubits = utils.createQureg()(2, env) state = np.array([0, 1, 0, 0]) cheat.initStateFromAmps()(qubits, np.real(state), np.imag(state)) pyquest_call_operation(op, qubits, classical_bit_registers=test_dict, classical_float_registers=dict(), classical_complex_registers=dict(), output_bit_register_dict=dict(),) utils.destroyQureg()(qubits, env=env) utils.destroyQuestEnv()(env) assert test_dict['test'][0]
# # Unless required by applicable law or agreed to in writing, software distributed under the License # is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express # or implied. See the License for the specific language governing permissions and limitations under # the License. import pytest import sys import numpy as np import numpy.testing as npt from qoqo import operations as ops from qoqo import Circuit from typing import List @pytest.mark.parametrize('measurement', [ (ops.MeasureQubit(qubit=0, readout='ro', readout_index=0), int, 0), (ops.PragmaRepeatedMeasurement(readout='ro', number_measurements=10), int, 0), (ops.PragmaGetPauliProduct( qubit_paulis={ 0: 1, 1: 2 }, readout='ro', circuit=Circuit()), float, 1), (ops.PragmaGetOccupationProbability(readout='ro', circuit=Circuit()), float, 1), (ops.PragmaGetStateVector(readout='ro', circuit=Circuit()), complex, 2), (ops.PragmaGetDensityMatrix(readout='ro', circuit=Circuit()), complex, 2), ]) def test_circuit(measurement): """Test building a circuit""" circuit = Circuit()
ops.PragmaSetStateVector(np.array([0, 1], dtype=complex)), ops.PragmaSetDensityMatrix(np.array([[0, 0], [0, 1]], dtype=complex)), ops.PragmaDamping(0, 0.005, 0.02), ops.PragmaDephasing(0, 0.005, 0.02), ops.PragmaDepolarising(0, 0.005, 0.02), ops.PragmaRandomNoise(0, 0.005, 0.02, 0.01), ops.PragmaGeneralNoise( 0, 0.005, np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=float)), ops.PragmaConditional('ro', 0, Circuit()), ops.PragmaRepeatGate(3), ops.PragmaBoostNoise(0.004), ops.PragmaStopParallelBlock([0, 1], 0.002), ops.PragmaSleep([0, 1], 0.002), ops.PragmaActiveReset(0), ops.PragmaGlobalPhase(0.03), ops.MeasureQubit(0, 'ro', 0), ops.PragmaRepeatedMeasurement('ro', 20, {}), ops.PragmaGetStateVector('ro', Circuit()), ops.PragmaGetDensityMatrix('ro', Circuit()), ops.PragmaGetOccupationProbability('ro', Circuit()), ops.PragmaGetPauliProduct({}, 'ro', Circuit()), ops.PragmaStartDecompositionBlock([0, 1], {}), ops.PragmaStopDecompositionBlock([0, 1]), ops.InputSymbolic('other', 0), ]) def test_all_operations(op: Any): """Test all operations with mocked interface""" circuit = Circuit() circuit += ops.DefinitionFloat(name='ro', length=1, is_output=False) circuit += ops.DefinitionBit(name='ro', length=1, is_output=False) circuit += ops.DefinitionComplex(name='ro', length=1, is_output=False)
def test_conditional_pragma(): """Test PRAGMA conditional operation""" test_dict = {'ro': [False, False], 'cond': [True, False]} circuit0 = Circuit() circuit0 += ops.PauliX(qubit=0) circuit0 += ops.MeasureQubit(readout='ro', qubit=0, readout_index=0) circuit1 = Circuit() circuit1 += ops.PauliX(qubit=1) circuit1 += ops.MeasureQubit(readout='ro', qubit=1, readout_index=1) operation = ops.PragmaConditional( condition_register='cond', condition_index=0, circuit=circuit0) env = utils.createQuestEnv()() qureg = utils.createQureg()(2, env) test_dict = {'ro': [False, False], 'cond': [True, False]} pyquest_call_operation(operation=operation, qureg=qureg, classical_bit_registers=test_dict, classical_float_registers=dict(), classical_complex_registers=dict(), output_bit_register_dict=dict(), ) utils.destroyQureg().call_interactive(qureg, env) utils.destroyQuestEnv().call_interactive(env) assert test_dict['ro'] == [True, False] operation = ops.PragmaConditional( condition_register='cond', condition_index=0, circuit=circuit1) env = utils.createQuestEnv()() qureg = utils.createQureg()(2, env) test_dict = {'ro': [False, False], 'cond': [True, False]} pyquest_call_operation(operation=operation, qureg=qureg, classical_bit_registers=test_dict, classical_float_registers=dict(), classical_complex_registers=dict(), output_bit_register_dict=dict(), ) utils.destroyQureg().call_interactive(qureg, env) utils.destroyQuestEnv().call_interactive(env) assert test_dict['ro'] == [False, True] operation = ops.PragmaConditional( condition_register='not_in', condition_index=0, circuit=circuit1) env = utils.createQuestEnv()() qureg = utils.createQureg()(2, env) with pytest.raises(RuntimeError): pyquest_call_operation(operation=operation, qureg=qureg, classical_bit_registers=dict(), classical_float_registers=dict(), classical_complex_registers=test_dict, output_bit_register_dict=dict(), ) utils.destroyQureg().call_interactive(qureg, env) utils.destroyQuestEnv().call_interactive(env)
(ops.CNOT(0, 1), 'cx q[0],q[1]'), (ops.Hadamard(0), 'h q[0]'), (ops.PauliX(0), 'x q[0]'), (ops.PauliY(0), 'y q[0]'), (ops.PauliZ(0), 'z q[0]'), (ops.SGate(0), 's q[0]'), (ops.TGate(0), 't q[0]'), (ops.SqrtPauliX(0), 'rx(1.5707963267948966) q[0]'), (ops.MolmerSorensenXX(0, 1), 'rxx(pi/2) q[0],q[1]'), (ops.ControlledPauliY(0, 1), 'cy q[0],q[1]'), (ops.ControlledPauliZ(0, 1), 'cz q[0],q[1]'), (ops.SingleQubitGate(0, 1, 0, 1, 0, 1.0), 'u3(0.0,0.0,-0.0) q[0]'), (ops.PragmaRepeatedMeasurement('ro', 1, None), 'measure q -> ro;\n'), (ops.PragmaRepeatedMeasurement('ro', 1, {0: 1, 1: 0}), 'measure q[1] -> ro[0];\nmeasure q[0] -> ro[1];\n'), (ops.MeasureQubit(0, 'ro', 0), 'measure q[0] -> ro[0]'), (ops.DefinitionFloat(name='ro', length=1, is_output=True), 'creg ro[1]'), (ops.DefinitionUsize(name='ro', length=1, is_output=True), 'creg ro[1]'), (ops.DefinitionBit(name='ro', length=1, is_output=True), 'creg ro[1]'), (ops.DefinitionComplex(name='ro', length=1, is_output=True), 'creg ro[1]'), (ops.InputSymbolic('other', 0), None), (ops.PragmaSetNumberOfMeasurements(20, 'ro'), None) ]) def test_gate_translation(gate: Tuple[Any, str]): """Test gate operations with QASM interface""" qasm_operation = qasm_call_operation(operation=gate[0], number_qubits=2) if gate[1] is None: assert qasm_operation is None else: