Skip to content

Commit

Permalink
Adding a random gate
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHimanshuc committed May 27, 2019
1 parent b155373 commit 91f0012
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 14 deletions.
32 changes: 22 additions & 10 deletions qiskit/converters/ast_to_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
from qiskit.extensions.standard.crz import CrzGate
from qiskit.extensions.standard.cu3 import Cu3Gate
from qiskit.extensions.standard.rzz import RZZGate
from qiskit.extensions.standard.i import IGate


def ast_to_dag(ast):
Expand Down Expand Up @@ -81,6 +82,7 @@ class AstInterpreter:
"u1": U1Gate,
"u2": U2Gate,
"u3": U3Gate,
"i": IGate,
"x": XGate,
"y": YGate,
"z": ZGate,
Expand Down Expand Up @@ -176,7 +178,8 @@ def _process_custom_unitary(self, node):
self.bit_stack.append({gbits[j]: bits[j][element[j]]
for j in range(len(gbits))})
self._create_dag_op(name,
[self.arg_stack[-1][s].sym() for s in gargs],
[self.arg_stack[-1][s].sym()
for s in gargs],
[self.bit_stack[-1][s] for s in gbits])
self.arg_stack.pop()
self.bit_stack.pop()
Expand All @@ -196,7 +199,8 @@ def _process_gate(self, node, opaque=False):
de_gate["n_args"] = node.n_args()
de_gate["n_bits"] = node.n_bits()
if node.n_args() > 0:
de_gate["args"] = [element.name for element in node.arguments.children]
de_gate["args"] = [
element.name for element in node.arguments.children]
else:
de_gate["args"] = []
de_gate["bits"] = [c.name for c in node.bitlist.children]
Expand All @@ -217,11 +221,14 @@ def _process_cnot(self, node):
maxidx = max([len(id0), len(id1)])
for idx in range(maxidx):
if len(id0) > 1 and len(id1) > 1:
self.dag.apply_operation_back(CXBase(), [id0[idx], id1[idx]], [], self.condition)
self.dag.apply_operation_back(
CXBase(), [id0[idx], id1[idx]], [], self.condition)
elif len(id0) > 1:
self.dag.apply_operation_back(CXBase(), [id0[idx], id1[0]], [], self.condition)
self.dag.apply_operation_back(
CXBase(), [id0[idx], id1[0]], [], self.condition)
else:
self.dag.apply_operation_back(CXBase(), [id0[0], id1[idx]], [], self.condition)
self.dag.apply_operation_back(
CXBase(), [id0[0], id1[idx]], [], self.condition)

def _process_measure(self, node):
"""Process a measurement node."""
Expand All @@ -231,7 +238,8 @@ def _process_measure(self, node):
raise QiskitError("internal error: reg size mismatch",
"line=%s" % node.line, "file=%s" % node.file)
for idx, idy in zip(id0, id1):
self.dag.apply_operation_back(Measure(), [idx], [idy], self.condition)
self.dag.apply_operation_back(
Measure(), [idx], [idy], self.condition)

def _process_if(self, node):
"""Process an if node."""
Expand Down Expand Up @@ -291,7 +299,8 @@ def _process_node(self, node):
args = self._process_node(node.children[0])
qid = self._process_bit_id(node.children[1])
for element in qid:
self.dag.apply_operation_back(UBase(*args, element), self.condition)
self.dag.apply_operation_back(
UBase(*args, element), self.condition)

elif node.type == "cnot":
self._process_cnot(node)
Expand Down Expand Up @@ -322,7 +331,8 @@ def _process_node(self, node):
elif node.type == "reset":
id0 = self._process_bit_id(node.children[0])
for i, _ in enumerate(id0):
self.dag.apply_operation_back(Reset(), [id0[i]], [], self.condition)
self.dag.apply_operation_back(
Reset(), [id0[i]], [], self.condition)

elif node.type == "if":
self._process_if(node)
Expand Down Expand Up @@ -357,10 +367,12 @@ def _create_dag_op(self, name, params, qargs):
elif name in self.gates:
if self.gates[name]['opaque']:
# call an opaque gate
op = Gate(name=name, num_qubits=self.gates[name]['n_bits'], params=params)
op = Gate(
name=name, num_qubits=self.gates[name]['n_bits'], params=params)
else:
# call a custom gate
raise QiskitError('Custom non-opaque gates are not supported by as_to_dag module')
raise QiskitError(
'Custom non-opaque gates are not supported by as_to_dag module')
else:
raise QiskitError("unknown operation for ast node name %s" % name)

Expand Down
1 change: 1 addition & 0 deletions qiskit/extensions/standard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
from .u3 import U3Gate
from .ubase import UBase
from .x import XGate
from .i import IGate
from .y import YGate
from .z import ZGate
from .rx import RXGate
Expand Down
1 change: 1 addition & 0 deletions qiskit/extensions/standard/ch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from qiskit.circuit import QuantumCircuit
from qiskit.circuit import QuantumRegister
from qiskit.extensions.standard.x import XGate
from qiskit.extensions.standard.i import IGate
from qiskit.extensions.standard.h import HGate
from qiskit.extensions.standard.cx import CnotGate
from qiskit.extensions.standard.t import TGate
Expand Down
69 changes: 69 additions & 0 deletions qiskit/extensions/standard/i.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-

# This code is part of Qiskit.
#
# (C) Copyright IBM 2017.
#
# This code is licensed under the Apache License, Version 2.0. You may
# obtain a copy of this license in the LICENSE.txt file in the root directory
# of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.
#
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""
Pauli X (bit-flip) gate.
Identity operator
"""

import numpy

from qiskit.circuit import CompositeGate
from qiskit.circuit import Gate
from qiskit.circuit import QuantumCircuit
from qiskit.circuit import QuantumRegister
from qiskit.qasm import pi
from qiskit.extensions.standard.u3 import U3Gate


class IGate(Gate):
"""Pauli X (bit-flip) gate."""

def __init__(self, label=None):
"""Create new X gate."""
super().__init__("i", 1, [], label=label)

def _define(self):
"""
gate x a {
u3(pi,0,pi) a;
}
"""
definition = []
q = QuantumRegister(1, "q")
rule = [
(U3Gate(pi, 0, pi), [q[0]], [])
]
for inst in rule:
definition.append(inst)
self.definition = definition

def inverse(self):
"""Invert this gate."""
return IGate() # self-inverse

def to_matrix(self):
"""Return a Numpy.array for the X gate."""
return numpy.array([[1, 0],
[0, i]], dtype=complex)


def i(self, q):
"""Apply X to q."""
return self.append(IGate(), [q], [])


QuantumCircuit.i = i
CompositeGate.i = i
4 changes: 2 additions & 2 deletions qiskit/quantum_info/operators/pauli.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,8 +310,8 @@ def to_operator(self):
def to_instruction(self):
"""Convert to Pauli circuit instruction."""
from qiskit.circuit import QuantumCircuit, QuantumRegister
from qiskit.extensions.standard import IdGate, XGate, YGate, ZGate
gates = {'I': IdGate(), 'X': XGate(), 'Y': YGate(), 'Z': ZGate()}
from qiskit.extensions.standard import IdGate, XGate, YGate, ZGate, IGate
gates = {'I': IdGate(), 'X': XGate(), 'Y': YGate(), 'Z': ZGate() , 'i' : IGate()}
label = self.to_label()
n_qubits = self.numberofqubits
qreg = QuantumRegister(n_qubits)
Expand Down
4 changes: 2 additions & 2 deletions test/python/quantum_info/test_synthesis.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from qiskit.circuit import QuantumCircuit, QuantumRegister
from qiskit.extensions import UnitaryGate
from qiskit.extensions.standard import (HGate, IdGate, SdgGate, SGate, U3Gate,
XGate, YGate, ZGate)
XGate, YGate, ZGate, IGate)
from qiskit.providers.basicaer import UnitarySimulatorPy
from qiskit.quantum_info.operators import Operator, Pauli
from qiskit.quantum_info.random import random_unitary
Expand All @@ -35,7 +35,7 @@

def make_oneq_cliffords():
"""Make as list of 1q Cliffords"""
ixyz_list = [g().to_matrix() for g in (IdGate, XGate, YGate, ZGate)]
ixyz_list = [g().to_matrix() for g in (IdGate, XGate, YGate, ZGate, IGate)]
ih_list = [g().to_matrix() for g in (IdGate, HGate)]
irs_list = [IdGate().to_matrix(),
SdgGate().to_matrix() @ HGate().to_matrix(),
Expand Down

0 comments on commit 91f0012

Please sign in to comment.