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

Fixed issue 4526 by adding InitializeGate #5179

Closed
wants to merge 10 commits into from

Conversation

incud
Copy link

@incud incud commented Oct 3, 2020

Summary

Fixes #4526

Details and comments

Initialize is implemented as an Instruction object instead of Gate because it uses reset instruction which is not unitary. This fix modifies Initialize passing an additional named parameter reset to its constructor whose default value is True. You can expect the behavior of Initialize to be the same if you don't specify the reset parameter.

However, instantiating Initialize with reset=False lets us define the InitializeGate gate. We can now create the controlled version of Initialize using InitializeGate(desired_vector).control(how_many_controlled_qubits).

Here an example usage:

from qiskit import * 
from qiskit.extensions.quantum_initializer.initializer import InitializeGate
import math

circuit = QuantumCircuit(2)
circuit.x(0)
wanted_state = [1 / math.sqrt(2), 1 / math.sqrt(2)]
circuit.append(InitializeGate(wanted_state).control(1), [0, 1])

result = execute(circuit, backend=Aer.get_backend('statevector_simulator'), shots=1).result()
print(result.get_statevector())

The introduction of InitializeGate and its controlled version may help with the implementation of quantum distance-based classifier like the one you find in [Schuld, Fingerhuth, Petruccione, 2018].

@incud incud requested a review from a team as a code owner October 3, 2020 14:12
@CLAassistant
Copy link

CLAassistant commented Oct 3, 2020

CLA assistant check
All committers have signed the CLA.

@ajavadia ajavadia added the on hold Can not fix yet label Oct 5, 2020
@ajavadia
Copy link
Member

ajavadia commented Oct 5, 2020

State preparation should really not be a unitary operation. Which part of the paper you link to above has this?

The reset could be removed by the transpiler if it occurs in the beginning of the circuit, but the reset is required to reliably prepare a state.

cc'ing @Cryoris too as he opened the initial issue.

@incud
Copy link
Author

incud commented Oct 6, 2020

@ajavadia thank you for your feedback

I've implemented the circuit performing controlled-initialize and twice-controlled-initialize in order to extend the circuit presented in the linked paper. In particular, when the author initializes its single-qubit data with the controlled version of RY (sometimes controlled X when the data is $|1\rangle$) I used my own controlled-initialize to initialize my multiple-qubits data.

@Cryoris
Copy link
Contributor

Cryoris commented Oct 12, 2020

After discussing with @ajavadia I think it would make sense to move the state-preparation logic into a Gate and have Initialize be only resets plus this state-preparation gate. I don't like the name InitializeGate though, this name only makes sense to us because we have a very specific definition of the term Gate and it will be weird for outside users to have this alongside Initialize. We can decide on naming later though.

@incud
Copy link
Author

incud commented Oct 25, 2020

@Cryoris Thank you for your comment.

There is anything I can do to improve the code?

@Cryoris
Copy link
Contributor

Cryoris commented Oct 26, 2020

Yes, could you put all the logic inside InitializeGate and have Initialize just be a layer of resets and then a call to InitializeGate? So schematically

class InitializeGate(Gate):
    def _define(self):
         # all the logic of `Initialize` in here

class Initialize(Instruction):
    def _define(self):
        # resets and then InitializeGate

Once that is done, we can think of a better name than InitializeGate 😄

Copy link
Contributor

@Cryoris Cryoris left a comment

Choose a reason for hiding this comment

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

I think the approach looks good. @ajavadia do you have an idea on the naming?

Comment on lines +304 to +312
"""Calculate a subcircuit that implements this initialization

Implements a recursive initialization algorithm, including optimizations,
from "Synthesis of Quantum Logic Circuits" Shende, Bullock, Markov
https://arxiv.org/abs/quant-ph/0406176v5

Additionally implements some extra optimizations: remove zero rotations and
double cnots.
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this comment can be removed, it applies to InitializeGate since the decomposition is computed there.

Copy link
Author

Choose a reason for hiding this comment

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

The comment is now removed

@incud
Copy link
Author

incud commented Oct 30, 2020

@Cryoris Thank you for your comment. I've merged the fix

@HuangJunye HuangJunye added the Community PR PRs from contributors that are not 'members' of the Qiskit repo label Jun 21, 2022
@prakharb10
Copy link
Contributor

Following #7666, now that we have a StatePreparation class, I believe that solves the issue this PR was intended for?

@jakelishman
Copy link
Member

jakelishman commented Jul 14, 2022

Yeah, this is obsolete as of #7666, thanks.

For the future: the motivating example for including the unitary component of initialisation as a separate instruction was so users could define their own mappings $\lvert\psi\rangle\to\lvert\phi\rangle$ more easily by using the state-prep unitary $\mathcal U_\chi\lvert0\rangle\coloneqq \lvert\chi\rangle$ as $\mathcal U_\phi\mkern2mu\mathcal U_\psi^\dagger$. So Initialize is now an Instruction that uses a StatePreparation Gate interally, where the "state preperation" operation is just defined to map $\lvert0\rangle\to\lvert\psi\rangle$.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Community PR PRs from contributors that are not 'members' of the Qiskit repo on hold Can not fix yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

Enable controlled initialization: derive Initialize from Gate
7 participants