Beispiel #1
0
    def new_scaling_function(circuit: QPROGRAM, *args: Any,
                             **kwargs: Any) -> QPROGRAM:
        # Convert to Mitiq representation.
        mitiq_circuit, input_circuit_type = convert_to_mitiq(circuit)

        # Scale the noise in the circuit.
        scaled_circuit = noise_scaling_function(mitiq_circuit, *args, **kwargs)

        if kwargs.get("return_mitiq") is True:
            return scaled_circuit

        # Base conversion back to input type.
        scaled_circuit = convert_from_mitiq(scaled_circuit, input_circuit_type)

        # Keep the same register structure in Qiskit circuits.
        if input_circuit_type == "qiskit":
            from mitiq.mitiq_qiskit.conversions import _transform_registers

            _transform_registers(
                scaled_circuit,
                new_qregs=circuit.qregs,
                new_cregs=circuit.cregs if scaled_circuit.cregs else None,
            )
            if circuit.cregs and not scaled_circuit.cregs:
                scaled_circuit.add_register(*circuit.cregs)

        return scaled_circuit
Beispiel #2
0
    def new_scaling_function(
        circuit: QPROGRAM, *args: Any, **kwargs: Any
    ) -> QPROGRAM:
        scaled_circuit = atomic_converter(noise_scaling_function)(
            circuit, *args, **kwargs
        )

        # Keep the same register structure and measurement order with Qiskit.
        if "qiskit" in scaled_circuit.__module__:
            from mitiq.mitiq_qiskit.conversions import (
                _transform_registers,
                _measurement_order,
            )

            scaled_circuit.remove_final_measurements()
            _transform_registers(
                scaled_circuit,
                new_qregs=circuit.qregs,
                new_cregs=circuit.cregs if scaled_circuit.cregs else None,
            )
            if circuit.cregs and not scaled_circuit.cregs:
                scaled_circuit.add_register(*circuit.cregs)

            for q, c in _measurement_order(circuit):
                scaled_circuit.measure(q, c)

        return scaled_circuit
Beispiel #3
0
def test_transform_qregs_two_qubit_ops(new_reg_sizes):
    nqubits = sum(new_reg_sizes)
    circ = to_qiskit(
        cirq.testing.random_circuit(nqubits,
                                    n_moments=5,
                                    op_density=1,
                                    random_state=1))
    orig = circ.copy()

    new_qregs = [qiskit.QuantumRegister(s) for s in new_reg_sizes]
    _transform_registers(circ, new_qregs=new_qregs)

    assert circ.qregs == new_qregs
    assert circ.cregs == orig.cregs
    assert _equal(from_qiskit(circ), from_qiskit(orig))
Beispiel #4
0
def test_transform_cregs(nbits, with_ops, measure):
    qreg = qiskit.QuantumRegister(nbits)
    creg = qiskit.ClassicalRegister(nbits)
    circ = qiskit.QuantumCircuit(qreg, creg)
    if with_ops:
        circ.h(qreg)
    if measure:
        circ.measure(qreg, creg)

    orig = circ.copy()

    new_cregs = [qiskit.ClassicalRegister(1) for _ in range(nbits)]
    _transform_registers(circ, new_cregs=new_cregs)

    assert circ.cregs == new_cregs
    assert circ.qregs == orig.qregs
    assert _equal(from_qiskit(circ), from_qiskit(orig))
Beispiel #5
0
def test_transform_qregs_and_cregs_random_circuit(new_reg_sizes, measure):
    nbits = sum(new_reg_sizes)
    circ = to_qiskit(
        cirq.testing.random_circuit(nbits,
                                    n_moments=5,
                                    op_density=1,
                                    random_state=10))
    creg = qiskit.ClassicalRegister(nbits)
    circ.add_register(creg)
    if measure:
        circ.measure(circ.qregs[0], creg)
    orig = circ.copy()

    new_qregs = [qiskit.QuantumRegister(s) for s in new_reg_sizes]
    new_cregs = [qiskit.ClassicalRegister(s) for s in new_reg_sizes]
    _transform_registers(circ, new_qregs=new_qregs, new_cregs=new_cregs)

    assert circ.qregs == new_qregs
    assert circ.cregs == new_cregs
    assert _equal(from_qiskit(circ), from_qiskit(orig))
Beispiel #6
0
def test_transform_registers_wrong_bit_number():
    nqubits = 2
    circ = qiskit.QuantumCircuit(qiskit.QuantumRegister(nqubits))
    new_qregs = [qiskit.QuantumRegister(1) for _ in range(2 * nqubits)]

    with pytest.raises(ValueError):
        _transform_registers(circ, new_qregs=new_qregs)

    new_cregs = [qiskit.ClassicalRegister(1)]
    with pytest.raises(ValueError):
        _transform_registers(circ, new_cregs=new_cregs)

    with pytest.raises(ValueError):
        _transform_registers(circ, new_qregs, new_cregs)