Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Hot answers tagged

6

Yes, it is possible to create controlled gates with an exponent in Cirq. For the specific case of the Z gate, Cirq includes a dedicated CZ gate that can be raised to a power: cs = cirq.CZ**0.5 More generally, cirq.ControlledGate works on any gate. It's a bit clunkier than the dedicated gates, but it does support being raised to a power (as long as the ...


6

Take a look again at the Hamiltonian, which is $$ H = \sum_{\langle i, j \rangle} J_{i j} Z_i Z_j + \sum_{i} h_i Z_i $$ Then notice that ZPowGate is generated by the the Pauli Z operator, and CZPowGate is equivalent to an operator generated by $Z \otimes Z$ up to single-qubit rotations. The idea is that Step 2 of the ansatz corresponds to applying a pulse ...


5

Cirq uses numpy's pseudo random number generator to pick measurement results, e.g. here is code from XmonStepper.simulate_measurement: def simulate_measurement(self, index: int) -> bool: [...] prob_one = np.sum(self._pool.map(_one_prob_per_shard, args)) result = bool(np.random.random() <= prob_one) [...] Cirq ...


5

In Cirq v0.5.0 and later you can use the controlled_by method on any Operation: op = cirq.X(target_qubit).controlled_by(control_qubit) You can also use controlled_by on any Gate, i.e. before specifying the target qubits: op = cirq.X.controlled_by(control_qubit).on(target_qubit) And you can also make a controlled version of the gate with an initially ...


5

Cirq distinguishes between "running" a circuit, which is generally supposed to act like hardware would (e.g. only getting samples), and "simulating" a circuit, which has more freedom. Most "simulate" methods, like cirq.Simulator().simulate(...) have a parameter initial_state which can either be a computational basis state (specified as an integer e.g. ...


4

This is actually very easy in Cirq. The controlled_by method can be used to automatically make any given gate controlled by an arbitrary number of control qubits. Here is a simple example for creating an X gate with 5 controls: import cirq qb = [cirq.LineQubit(i) for i in range(6)] cnX = cirq.X.controlled_by(qb[0], qb[1], qb[2], qb[3], qb[4]); circuit = ...


4

When using a simulator, it doesn't really matter what kind of qubit you refer to. You can even mix-and-match the types. The type of qubit only becomes relevant when you intend to run on a device, because devices have qubits at specific locations. For example, if you wanted to run on Bristlecone, you would limit yourself to GridQubit instances that actually ...


3

If you are looking for a more complete implementation of a quantum variational algorithm in the context of Cirq, I would recommend looking at the second example in the OpenFermion-Cirq notebook found here. It uses a custom ansatz for hydrogen in a minimal basis, but makes a bit more explicit all the required pieces. Another good example, perhaps without ...


3

If you call initialize in this case, you will be specifying a general state in $\mathbb{C}^8$. However what you have is more specialized. For example only having 4 nonzero amplitudes. So the call to initialize won't know this a priori. So it won't realize the initialization circuit can be decomposed easily. Or at least it will need to do some extra ...


3

The Fourier transform part (everything from the swaps onward) looks correct. The initialization (column of Hadamards) looks correct. But the part where you do controlled modular multiplications doesn't, because there's no operations controlled on the 2nd through fifth qubits that you are QFT-ing. You also seem to expect the output to be the period, when ...


3

Looking at the documentation and the GitHub, there is a something called ControlledGate. This class is said to augment existing gates with a control qubit. You can look at the test file. I can see line 72 : cxa = cirq.ControlledGate(cirq.X**cirq.Symbol('a')) Could you try: gate = cirq.ControlledGate(cirq.X**0.5) ?


3

This is going to change somewhat radically in the next version of cirq, so I'll give an answer for both versions. In v0.3, in order for a simulator to understand a custom gate, the gate must implement either cirq.CompositeGate or cirq.KnownMatrix. For your case, the simplest is to implement the matrix: # assuming cirq v0.3 import cirq import numpy as np ...


2

I searched for doing a custom gate on the Cirq documentation and here are the results : Gate sets The xmon simulator is designed to work with operations that are either a GateOperation applying an XmonGate, a CompositeOperation that decomposes (recursively) to XmonGates, or a 1-qubit or 2-qubit operation with a KnownMatrix. By default the ...


2

You can test stand alone the a modular multiplication circuit. In this case $\text{base} = 2$ and $N = 3$. However the smallest useful composite $N = 15 = 3 \times 5$. Let's take a well known Multiplication by 7 modulo 15 circuit We start with input $$\ |1\rangle \text{ gives } |7\rangle$$ $$\ |7\rangle \text{ gives } |4\rangle$$ $$\ |4\rangle \text{ gives ...


2

The endian-ness of the qubits is the answer. Both QFT and phase estimation rely on certain endianness of the register, and the representations used in the controlled-unitary part has to match the endianness used in the QFT part (and in the answer). This circuit produces the expected outcome with the inverse QFT block:


2

In the current release of Cirq (0.4.0) there is a strong limitation on symbols: you can't scale them or add them (Why? We were worried about being pulled down the rabbit hole of implementing a whole symbolic algebra system.). Making matters worse, Cirq internally works in radians divided by pi to avoid some minor sources of floating point error. So when you ...


2

This is the matrix for $Z^t$: $$Z^t = \begin{bmatrix} 1&0\\0&(-1)^t \end{bmatrix} = \begin{bmatrix} 1&0\\0&e^{i \pi t} \end{bmatrix}$$ This is the matrix for $R_Z(\pi t)$: $$R_Z(\pi t) = e^{-iZt/2} = \begin{bmatrix} e^{-i \pi t / 2}&0\\0&e^{+i \pi t / 2} \end{bmatrix} = e^{-i \pi t/2} Z^t $$ Which means that $$Z^t \equiv R_Z(\pi ...


2

Note that $$RX(\phi) = \begin{pmatrix} \cos(\phi/2) & -i\sin(\phi/2) \\-isin(\phi/2) & \cos(\phi/2)\end{pmatrix}$$ Then $$RX(\pi q) = \begin{pmatrix} \cos(\pi q/2) & -i\sin(\pi q/2) \\-isin(\pi q/2) & \cos(\pi q/2)\end{pmatrix}.$$ Now, using that $\cos(\pi k + \pi/2) = 0 = \sin(\pi k)$ and $\cos(\pi k) = 1 = \sin(\pi k + \pi/2)$ for $k\in \...


2

You can initialize a quantum state by using the QuantumCircuit.initialize() function. For example, to initialize a circuit into the state |1>, we can perform the initialization as follows : vector = [0,1] qr = QuantumRegister(1) qc = QuantumCircuit(qr) qc.initialize(vector, [qr[0]]) There is more detail about how to use it in this tutorial


2

I am definitely biased (writing a book on quantum computing with Python and Q#), but I am a Pythonista and love using Q#. The design of the language is good for long term quantum computing development; it allows you to think more at the algorithmic level, not at the assembly level as many other quantum programming languages are targeting. It has a Jupyter ...


1

I would suggest to start with Quirk as it offers a drag-and-drop circuit model. Furthermore, Quirk offers some subroutines such as basic arithmetic operation (on integers) and allows to easily define new subroutines. (All drag an drop!) It can simulate up to 17 (?) qubits. Once you want to go beyond an "easy" circuit representation I suggest Microsofts Q#. ...


1

In my opinion, at the moment, qiskit is the most suitable one for learning and teaching. For a basic introductory material (we have used it in 14 two-day or three-day workshops), I recommend the following repo: https://gitlab.com/qkitchen/basics-of-quantum-computing the link to workshops: https://qsoftware.lu.lv/index.php/workshops/


1

Cirq's simulator is a state vector simulator, which cannot be told to focus on the amplitude of a specific output state or combination of output states. Some tensor network based simulators can get benefit from focusing on a specific output state, but there isn't such a simulator in Cirq.


1

cirq.ZPowGate(exponent=t/pi).controlled_by(control_qubit).on(target_qubit) or equivalently cirq.CZ(control_qubit, target_qubit)**(t / pi)


1

If you want a random computational basis state, set the input state to the integer random.randint(0, 2**qubits-1). If you want a random superposition sampled from the Haar measure, there is a method cirq.testing.random_superposition(dim=2**qubits). Once you have created your initial state, you pass it into the simulator like cirq.Simulator().simulate(...


1

The following code snippet will do almost what you want: class MyLayerGate(cirq.Gate): def _decompose_(self, qubits): a, b, c = qubits return my_layer(a, b, c) # [will be unnecessary in v0.5.0] workaround for cirq.unitary ignoring _decompose_: def _unitary_(self): return cirq.unitary( cirq.Circuit.from_ops(...


1

You have to put an extra SWAP-gate after the QFT, see this circuit. Furthermore, the two controlled-Z gates on the same qubit are not necessary. This can reduce the circuit further to this.


1

This looks right, although I would emphasise that it is not really best practice to have to ask this question at this stage. The whole point of doing a particularly simple example is so that you can confirm that it's doing what you've already calculated analytically. It's quite important to do the analytic bit first to avoid confirmation bias. Anyway, let's ...


Only top voted, non community-wiki answers of a minimum length are eligible