Convert to cirq

For example, we can translate a simple quantum algorithm to Cirq and print the final cirq.Circuit:

import cirq
import numpy as np
from cirqprojectq.circ_engine import CIRQ
import projectq
qubits = [cirq.google.XmonQubit(0, i) for i in range(2)]
CIRQ = CIRQ(qubits=qubits)
eng = projectq.MainEngine(backend=CIRQ)
qureg = eng.allocate_qureg(len(qubits))
eng.flush()
projectq.ops.Rx(0.25 * np.pi) | qureg[0]
projectq.ops.Ry(0.5 * np.pi) | qureg[1]
projectq.ops.Rz(0.5 * np.pi) | qureg[0]
projectq.ops.H | qureg[1]
projectq.ops.C(projectq.ops.X) | (qureg[0], qureg[1])
projectq.ops.Z | qureg[0]
projectq.ops.X | qureg[0]
projectq.ops.Y | qureg[0]
eng.flush()
print(CIRQ.circuit)
(0, 0): ───X^0.25───Z^0.5───────@───Z───X───Y───
                            │
(0, 1): ────────────Y^0.5───H───X───────────────

The backend

Provides a projectq engine that translates a projectq circuit to a cirq circuit.

class cirqprojectq.circ_engine.CIRQ(qubits=None, device=None, rules=None, strategy=cirq.InsertStrategy.EARLIEST)[source]

Bases: projectq.cengines._basics.BasicEngine

A projectq backend designated to translating to cirq.

Parameters:
  • qubits (list(cirq.devices.grid_qubit)) – the qubits
  • device (cirq.devices.Device) – a device that provides the qubits.
  • rules (cirqprojectq._rules_pq_to_cirq.Ruleset_pq_to_cirq) – rule set
  • strategy (cirq.circuits.InsertStrategy) – Insert strategy in cirq.
circuit

cirq.Circuit – the circuit stored in the engine.

device

cirq.devices.Device – A device. Currently not used.

is_available(cmd)[source]

Returns true if the command can be translated.

Parameters:cmd (Command) – Command for which to check availability
qubits

list(cirq.QubitID) – The cirq qubits used in the circuit.

receive(command_list)[source]

Receives a command list and, for each command, stores it until completion.

Parameters:command_list – List of commands to execute
reset(keep_map=True)[source]

Resets the engine.

Translation rules

This file provides classes to store rules for translating ProjectQ to Cirq operations.

class cirqprojectq._rules_pq_to_cirq.Rule_pq_to_cirq(classes, translation)[source]

Bases: object

class cirqprojectq._rules_pq_to_cirq.Ruleset_pq_to_cirq(rules=[])[source]

Bases: object

add_rule(rule)[source]

Add a single rule to the set of known rules.

Parameters:rule (Rule_pq_to_cirq) – a rule that can be used for translations.
add_rules(rules=[])[source]

Add rules to the set of known rules.

Parameters:rules (list of Rule_pq_to_cirq) – the rules that can be used for translations.
known_rules

Dictionary of known translations.

translate(cmd, mapping, qubits)[source]

Translate a projectq operation into a Cirq operation.

Parameters:
  • cmd (projectq.ops.Command) – a projectq command instance
  • mapping (dict) – a dictionary of qubit mappings
  • (list of (qubits) – class:cirq.QubitID`): cirq qubits
Returns:

cirq.Operation

Rules for common gates

This module provides translation rules from Projectq to Cirq for some common gates.

Rules for xmon gates

This module provides translation rules from Xmon gates in Projectq to Xmon gates in Cirq.