Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make unrealized SingleQubitQPDGate have definition of None rather than raise error #442

Merged
merged 6 commits into from
Nov 2, 2023

Conversation

garrison
Copy link
Member

@garrison garrison commented Oct 27, 2023

@garrison garrison added the cutting QPD-based circuit cutting code label Oct 27, 2023
@garrison garrison changed the title Make unrealized SingleQubitQPDGate has definition of None rather than error Make unrealized SingleQubitQPDGate have definition of None rather than raise error Oct 27, 2023
Comment on lines -104 to -108
@abstractmethod
def _define(self) -> None:
"""Generate a decomposed gate."""
raise NotImplementedError # pragma: no cover

Copy link
Member Author

@garrison garrison Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing these lines wasn't essential, but seems more in line with the idea of relying on the base class's Instruction._define to do nothing rather than to raise an error if a definition isn't explicitly provided.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense to me, thanks.

@garrison garrison marked this pull request as draft October 27, 2023 19:22
@garrison
Copy link
Member Author

garrison commented Oct 27, 2023

Come to think of it, making this change might allow us to avoid having to override the _directive property, as well.

https://github.com/Qiskit-Extensions/circuit-knitting-toolbox/blob/2df09335477469c949cf714f7cf6210cc40ebdf8/circuit_knitting/cutting/qpd/instructions/qpd_gate.py#L210-L213

UPDATE: I tried removing the _directive override, but I got a test failure.

=================================== FAILURES ===================================
___________________ test_transpile_before_realizing_basis_id ___________________

    def test_transpile_before_realizing_basis_id():
        """Test a workflow where a :class:`.SingleQubitQPDGate` is passed through the transpiler."""
        circuit = EfficientSU2(4, entanglement="linear", reps=2).decompose()
        circuit.assign_parameters([0.8] * len(circuit.parameters), inplace=True)
        observables = PauliList(["ZZII"])
        subcircuits, bases, subobservables = partition_problem(
            circuit=circuit, partition_labels="AABB", observables=observables
        )
    
        # Create a fake backend, and modify the target gate set so it thinks a
        # SingleQubitQPDGate is allowed.
        backend = FakeLagosV2()
        target = deepcopy(backend.target)
        sample_qpd_instruction = SingleQubitQPDGate(QPDBasis.from_instruction(CXGate()), 1)
        target.add_instruction(
            sample_qpd_instruction,
            {(i,): None for i in range(target.num_qubits)},
        )
        pass_manager = generate_preset_pass_manager(3, target=target)
    
        # Pass each subcircuit through the pass manager.
>       subcircuits = {
            label: pass_manager.run(subcircuits["A"])
            for label, circuit in subcircuits.items()
        }

test/cutting/test_cutting_workflows.py:54: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
test/cutting/test_cutting_workflows.py:55: in <dictcomp>
    label: pass_manager.run(subcircuits["A"])
.tox/py/lib/python3.11/site-packages/qiskit/transpiler/passmanager.py:537: in run
    return super().run(circuits, output_name, callback)
.tox/py/lib/python3.11/site-packages/qiskit/transpiler/passmanager.py:231: in run
    return self._run_single_circuit(circuits, output_name, callback)
.tox/py/lib/python3.11/site-packages/qiskit/transpiler/passmanager.py:292: in _run_single_circuit
    result = running_passmanager.run(circuit, output_name=output_name, callback=callback)
.tox/py/lib/python3.11/site-packages/qiskit/transpiler/runningpassmanager.py:125: in run
    dag = self._do_pass(pass_, dag, passset.options)
.tox/py/lib/python3.11/site-packages/qiskit/transpiler/runningpassmanager.py:169: in _do_pass
    dag = self._do_pass(required_pass, dag, options)
.tox/py/lib/python3.11/site-packages/qiskit/transpiler/runningpassmanager.py:173: in _do_pass
    dag = self._run_this_pass(pass_, dag)
.tox/py/lib/python3.11/site-packages/qiskit/transpiler/runningpassmanager.py:227: in _run_this_pass
    pass_.run(FencedDAGCircuit(dag))
.tox/py/lib/python3.11/site-packages/qiskit/transpiler/passes/optimization/commutation_analysis.py:75: in run
    does_commute = self.comm_checker.commute(
.tox/py/lib/python3.11/site-packages/qiskit/circuit/commutation_checker.py:141: in commute
    operator_1 = Operator(op1, input_dims=(2,) * len(qarg1), output_dims=(2,) * len(qarg1))
.tox/py/lib/python3.11/site-packages/qiskit/quantum_info/operators/operator.py:97: in __init__
    self._data = self._init_instruction(data).data
.tox/py/lib/python3.11/site-packages/qiskit/quantum_info/operators/operator.py:632: in _init_instruction
    op._append_instruction(instruction)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = Operator([[1.+0.j, 0.+0.j],
          [0.+0.j, 1.+0.j]],
         input_dims=(2,), output_dims=(2,))
obj = Instruction(name='qpd_1q', num_qubits=1, num_clbits=0, params=[])
qargs = None

    def _append_instruction(self, obj, qargs=None):
        """Update the current Operator by apply an instruction."""
        from qiskit.circuit.barrier import Barrier
        from .scalar_op import ScalarOp
    
        mat = self._instruction_to_matrix(obj)
        if mat is not None:
            # Perform the composition and inplace update the current state
            # of the operator
            op = self.compose(mat, qargs=qargs)
            self._data = op.data
        elif isinstance(obj, Barrier):
            return
        else:
            # If the instruction doesn't have a matrix defined we use its
            # circuit decomposition definition if it exists, otherwise we
            # cannot compose this gate and raise an error.
            if obj.definition is None:
>               raise QiskitError(f"Cannot apply Operation: {obj.name}")
E               qiskit.exceptions.QiskitError: 'Cannot apply Operation: qpd_1q'

.tox/py/lib/python3.11/site-packages/qiskit/quantum_info/operators/operator.py:676: QiskitError

@garrison garrison added the qpd Related to quasi-probability decompositions label Oct 27, 2023
@garrison garrison added the serialization Related to serialization of our custom objects label Oct 30, 2023
@garrison garrison marked this pull request as ready for review October 31, 2023 15:54
@coveralls
Copy link

Pull Request Test Coverage Report for Build 6709097724

  • 2 of 2 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.008%) to 92.377%

Totals Coverage Status
Change from base Build 6708466730: -0.008%
Covered Lines: 2957
Relevant Lines: 3201

💛 - Coveralls

ibrahim-shehzad
ibrahim-shehzad previously approved these changes Nov 2, 2023
Copy link
Collaborator

@ibrahim-shehzad ibrahim-shehzad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This resolution makes sense, thank you.

caleb-johnson
caleb-johnson previously approved these changes Nov 2, 2023
Copy link
Collaborator

@caleb-johnson caleb-johnson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks 👍

@garrison garrison merged commit 2410efd into main Nov 2, 2023
10 checks passed
@garrison garrison deleted the dont-define-unrealized-qpdgate branch November 2, 2023 20:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cutting QPD-based circuit cutting code qpd Related to quasi-probability decompositions serialization Related to serialization of our custom objects
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Serializing/Deserializing a circuit with QPD gates causes a ValueError
4 participants